Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// 256 bit ID generation with uniqueness of 2^32 and 2^224 strength
- /// </summary>
- private static uint Count256 = 0; // This is always incremented, reuse lastest value (on disk etc).
- public static byte[] GenerateUniqueID256()
- {
- byte[] Key = new byte[32];
- using (RNGCryptoServiceProvider crng = new RNGCryptoServiceProvider()) crng.GetBytes(Key);
- Array.Copy(BitConverter.GetBytes(Count256), 0, Key, 28, 4);
- BitArray ba = new BitArray(Key);
- for (int i = 0; i < 28; i++)
- {
- bool tmp = ba[8 * i + 7];
- ba[8 * i + 7] = ba[255 - i];
- ba[255 - i] = tmp;
- }
- for (int i = 0; i < 4; i++)
- {
- bool tmp = ba[32 * i + 8 * i + 3];
- ba[32 * i + 8 * i + 3] = ba[224 + i];
- ba[224 + i] = tmp;
- }
- ba.CopyTo(Key, 0);
- Count256++;
- return Key;
- }
- public static uint ResolveUserNumber256(byte[] Key)
- {
- BitArray ba = new BitArray(Key);
- for (int i = 0; i < 4; i++)
- {
- bool tmp = ba[32 * i + 8 * i + 3];
- ba[32 * i + 8 * i + 3] = ba[224 + i];
- ba[224 + i] = tmp;
- }
- for (int i = 27; i > -1; i--)
- {
- bool tmp = ba[8 * i + 7];
- ba[8 * i + 7] = ba[ba.Length - (1 + i)];
- ba[ba.Length - (1 + i)] = tmp;
- }
- byte[] bytes = new byte[Key.Length];
- ba.CopyTo(bytes, 0);
- return BitConverter.ToUInt32(new byte[] { bytes[28], bytes[29], bytes[30], bytes[31] }, 0);
- }
- /// <summary>
- /// 512 bit ID generation with uniqueness of 2^64 and 2^448 strength.
- /// </summary>
- private static ulong Count512 = 0; // This is always incremented, reuse lastest value (on disk etc).
- public static byte[] GenerateUniqueID512()
- {
- byte[] Key = new byte[64];
- using (RNGCryptoServiceProvider crng = new RNGCryptoServiceProvider()) crng.GetBytes(Key);
- Array.Copy(BitConverter.GetBytes(Count512), 0, Key, 56, 8);
- BitArray ba = new BitArray(Key);
- for (int i = 0; i < 56; i++)
- {
- bool tmp = ba[8 * i + 7];
- ba[8 * i + 7] = ba[512 - (1 + i)];
- ba[512 - (1 + i)] = tmp;
- }
- for (int i = 0; i < 8; i++)
- {
- bool tmp = ba[64 * i + 8 * i + 3];
- ba[64 * i + 8 * i + 3] = ba[448 + i];
- ba[448 + i] = tmp;
- }
- ba.CopyTo(Key, 0);
- Count512++;
- return Key;
- }
- public static ulong ResolveUserNumber512(byte[] Key)
- {
- BitArray ba = new BitArray(Key);
- for (int i = 0; i < 8; i++)
- {
- bool tmp = ba[64 * i + 8 * i + 3];
- ba[64 * i + 8 * i + 3] = ba[448 + i];
- ba[448 + i] = tmp;
- }
- for (int i = 55; i > -1; i--)
- {
- bool tmp = ba[8 * i + 7];
- ba[8 * i + 7] = ba[ba.Length - (1 + i)];
- ba[ba.Length - (1 + i)] = tmp;
- }
- byte[] bytes = new byte[Key.Length];
- ba.CopyTo(bytes, 0);
- return BitConverter.ToUInt64(new byte[] { bytes[56], bytes[57], bytes[58], bytes[59],
- bytes[60], bytes[61], bytes[62], bytes[63] }, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment