TizzyT

Unique ID Generation -TizzyT

Dec 11th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 KB | None | 0 0
  1.         /// <summary>
  2.         /// 256 bit ID generation with uniqueness of 2^32 and 2^224 strength
  3.         /// </summary>
  4.         private static uint Count256 = 0; // This is always incremented, reuse lastest value (on disk etc).
  5.         public static byte[] GenerateUniqueID256()
  6.         {
  7.             byte[] Key = new byte[32];
  8.             using (RNGCryptoServiceProvider crng = new RNGCryptoServiceProvider()) crng.GetBytes(Key);
  9.             Array.Copy(BitConverter.GetBytes(Count256), 0, Key, 28, 4);
  10.             BitArray ba = new BitArray(Key);
  11.             for (int i = 0; i < 28; i++)
  12.             {
  13.                 bool tmp = ba[8 * i + 7];
  14.                 ba[8 * i + 7] = ba[255 - i];
  15.                 ba[255 - i] = tmp;
  16.             }
  17.             for (int i = 0; i < 4; i++)
  18.             {
  19.                 bool tmp = ba[32 * i + 8 * i + 3];
  20.                 ba[32 * i + 8 * i + 3] = ba[224 + i];
  21.                 ba[224 + i] = tmp;
  22.             }
  23.             ba.CopyTo(Key, 0);
  24.             Count256++;
  25.             return Key;
  26.         }
  27.         public static uint ResolveUserNumber256(byte[] Key)
  28.         {
  29.             BitArray ba = new BitArray(Key);
  30.             for (int i = 0; i < 4; i++)
  31.             {
  32.                 bool tmp = ba[32 * i + 8 * i + 3];
  33.                 ba[32 * i + 8 * i + 3] = ba[224 + i];
  34.                 ba[224 + i] = tmp;
  35.             }
  36.             for (int i = 27; i > -1; i--)
  37.             {
  38.                 bool tmp = ba[8 * i + 7];
  39.                 ba[8 * i + 7] = ba[ba.Length - (1 + i)];
  40.                 ba[ba.Length - (1 + i)] = tmp;
  41.             }
  42.             byte[] bytes = new byte[Key.Length];
  43.             ba.CopyTo(bytes, 0);
  44.             return BitConverter.ToUInt32(new byte[] { bytes[28], bytes[29], bytes[30], bytes[31] }, 0);
  45.         }
  46.  
  47.         /// <summary>
  48.         /// 512 bit ID generation with uniqueness of 2^64 and 2^448 strength.
  49.         /// </summary>
  50.         private static ulong Count512 = 0; // This is always incremented, reuse lastest value (on disk etc).
  51.         public static byte[] GenerateUniqueID512()
  52.         {
  53.             byte[] Key = new byte[64];
  54.             using (RNGCryptoServiceProvider crng = new RNGCryptoServiceProvider()) crng.GetBytes(Key);
  55.             Array.Copy(BitConverter.GetBytes(Count512), 0, Key, 56, 8);
  56.             BitArray ba = new BitArray(Key);
  57.             for (int i = 0; i < 56; i++)
  58.             {
  59.                 bool tmp = ba[8 * i + 7];
  60.                 ba[8 * i + 7] = ba[512 - (1 + i)];
  61.                 ba[512 - (1 + i)] = tmp;
  62.             }
  63.             for (int i = 0; i < 8; i++)
  64.             {
  65.                 bool tmp = ba[64 * i + 8 * i + 3];
  66.                 ba[64 * i + 8 * i + 3] = ba[448 + i];
  67.                 ba[448 + i] = tmp;
  68.             }
  69.             ba.CopyTo(Key, 0);
  70.             Count512++;
  71.             return Key;
  72.         }
  73.         public static ulong ResolveUserNumber512(byte[] Key)
  74.         {
  75.             BitArray ba = new BitArray(Key);
  76.             for (int i = 0; i < 8; i++)
  77.             {
  78.                 bool tmp = ba[64 * i + 8 * i + 3];
  79.                 ba[64 * i + 8 * i + 3] = ba[448 + i];
  80.                 ba[448 + i] = tmp;
  81.             }
  82.             for (int i = 55; i > -1; i--)
  83.             {
  84.                 bool tmp = ba[8 * i + 7];
  85.                 ba[8 * i + 7] = ba[ba.Length - (1 + i)];
  86.                 ba[ba.Length - (1 + i)] = tmp;
  87.             }
  88.             byte[] bytes = new byte[Key.Length];
  89.             ba.CopyTo(bytes, 0);
  90.             return BitConverter.ToUInt64(new byte[] { bytes[56], bytes[57], bytes[58], bytes[59],
  91.                                                       bytes[60], bytes[61], bytes[62], bytes[63] }, 0);
  92.         }
Advertisement
Add Comment
Please, Sign In to add comment