diff options
author | Dennis Kobert <d-kobert@web.de> | 2019-06-11 23:38:13 +0200 |
---|---|---|
committer | Dennis Kobert <d-kobert@web.de> | 2019-06-11 23:38:13 +0200 |
commit | 2fa4a0e50ebfc97059c8b84dbd17e79f9afc8a8d (patch) | |
tree | c3b34ccb2737e347a73768536895cbbaab13cc01 /dsa/FireBase/FirebaseKeyGenerator.cs | |
parent | ec991104f56e90d7bb2878da2fe6ed4e585dfc46 (diff) | |
parent | af74efccf8d21e6151022b71f3cacd3fa83024ee (diff) |
Merge branch 'rework-backend'
Diffstat (limited to 'dsa/FireBase/FirebaseKeyGenerator.cs')
-rw-r--r-- | dsa/FireBase/FirebaseKeyGenerator.cs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/dsa/FireBase/FirebaseKeyGenerator.cs b/dsa/FireBase/FirebaseKeyGenerator.cs new file mode 100644 index 0000000..37beed5 --- /dev/null +++ b/dsa/FireBase/FirebaseKeyGenerator.cs @@ -0,0 +1,79 @@ +using System; +using System.Text; + +namespace Firebase.Database +{ + /// <summary> + /// Offline key generator which mimics the official Firebase generators. + /// Credit: https://github.com/bubbafat/FirebaseSharp/blob/master/src/FirebaseSharp.Portable/FireBasePushIdGenerator.cs + /// </summary> + public class FirebaseKeyGenerator + { + // Modeled after base64 web-safe chars, but ordered by ASCII. + private const string PushCharsString = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"; + private static readonly char[] PushChars; + private static readonly DateTimeOffset Epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero); + + private static readonly Random random = new Random(); + private static readonly byte[] lastRandChars = new byte[12]; + + // Timestamp of last push, used to prevent local collisions if you push twice in one ms. + private static long lastPushTime; + + static FirebaseKeyGenerator() + { + PushChars = Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(PushCharsString)); + } + + /// <summary> + /// Returns next firebase key based on current time. + /// </summary> + /// <returns> + /// The <see cref="string" />. + /// </returns> + public static string Next() + { + // We generate 72-bits of randomness which get turned into 12 characters and + // appended to the timestamp to prevent collisions with other clients. We store the last + // characters we generated because in the event of a collision, we'll use those same + // characters except "incremented" by one. + var id = new StringBuilder(20); + var now = (long) (DateTimeOffset.Now - Epoch).TotalMilliseconds; + var duplicateTime = now == lastPushTime; + lastPushTime = now; + + var timeStampChars = new char[8]; + for (var i = 7; i >= 0; i--) + { + var index = (int) (now % PushChars.Length); + timeStampChars[i] = PushChars[index]; + now = (long) Math.Floor((double) now / PushChars.Length); + } + + if (now != 0) throw new Exception("We should have converted the entire timestamp."); + + id.Append(timeStampChars); + + if (!duplicateTime) + { + for (var i = 0; i < 12; i++) lastRandChars[i] = (byte) random.Next(0, PushChars.Length); + } + else + { + // If the timestamp hasn't changed since last push, use the same random number, + // except incremented by 1. + var lastIndex = 11; + for (; lastIndex >= 0 && lastRandChars[lastIndex] == PushChars.Length - 1; lastIndex--) + lastRandChars[lastIndex] = 0; + + lastRandChars[lastIndex]++; + } + + for (var i = 0; i < 12; i++) id.Append(PushChars[lastRandChars[i]]); + + if (id.Length != 20) throw new Exception("Length should be 20."); + + return id.ToString(); + } + } +}
\ No newline at end of file |