Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement