Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- public static class CryptoUtils
- {
- public static string GetUniqueKey(int size = 64)
- {
- const int minSize = 5;
- const int maxSize = 64;
- char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
- byte[] data = new byte[1];
- if (size < minSize)
- size = minSize;
- if (size > maxSize)
- size = maxSize;
- StringBuilder result = new StringBuilder(size);
- using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
- {
- crypto.GetNonZeroBytes(data);
- data = new byte[size];
- crypto.GetNonZeroBytes(data);
- foreach (byte b in data)
- result.Append(chars[b % (chars.Length - 1)]);
- }
- return result.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement