Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // uses RNGCryptoServiceProvider to generate a fixed length API Key
- public static string CreateKey(int length)
- {
- string rchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdejghijklmnopqrstuvwxyz1234567890";
- StringBuilder Sb = new StringBuilder();
- using (RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider())
- {
- byte[] Buffer = new byte[sizeof(uint)];
- while (length-- > 0)
- {
- Rng.GetBytes(Buffer);
- uint N = BitConverter.ToUInt32(Buffer, 0);
- Sb.Append(rchars[(int)(N % (uint)rchars.Length)]);
- }
- }
- return Sb.ToString();
- }
- // inserts a non alphanumeric characters into a key generated by CreateKey
- // to output a more secure Password
- public static string CreatePassword()
- {
- string Chars = @"~`!@#$%^&*()\/',.?;:|";
- var RNG = new Random();
- // Select a character to insert into the password
- string Character = new String(Enumerable.Repeat(Chars, 1).Select(s => s[RNG.Next(s.Length)]).ToArray());
- // output a 9 character long password to which a single character will be added
- return CreateKey(9).Insert(RNG.Next(9), Character);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement