SHARE
TWEET

Untitled

a guest Apr 21st, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Linq;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4.  
  5. public static class CryptoUtils
  6. {
  7.     public static string GetUniqueKey(int size = 64)
  8.     {
  9.         const int minSize = 5;
  10.         const int maxSize = 64;
  11.         char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
  12.         byte[] data = new byte[1];
  13.         if (size < minSize)
  14.             size = minSize;
  15.         if (size > maxSize)
  16.             size = maxSize;
  17.         StringBuilder result = new StringBuilder(size);
  18.         using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
  19.         {
  20.             crypto.GetNonZeroBytes(data);
  21.             data = new byte[size];
  22.             crypto.GetNonZeroBytes(data);
  23.             foreach (byte b in data)
  24.                 result.Append(chars[b % (chars.Length - 1)]);
  25.         }
  26.         return result.ToString();
  27.     }
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top