Advertisement
Ortund

Untitled

Jul 5th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. // uses RNGCryptoServiceProvider to generate a fixed length API Key
  2. public static string CreateKey(int length)
  3. {
  4.     string rchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdejghijklmnopqrstuvwxyz1234567890";
  5.     StringBuilder Sb = new StringBuilder();
  6.  
  7.     using (RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider())
  8.     {
  9.         byte[] Buffer = new byte[sizeof(uint)];
  10.  
  11.         while (length-- > 0)
  12.         {
  13.             Rng.GetBytes(Buffer);
  14.             uint N = BitConverter.ToUInt32(Buffer, 0);
  15.             Sb.Append(rchars[(int)(N % (uint)rchars.Length)]);
  16.         }
  17.     }
  18.  
  19.     return Sb.ToString();
  20. }
  21.  
  22. // inserts a non alphanumeric characters into a key generated by CreateKey
  23. // to output a more secure Password
  24. public static string CreatePassword()
  25. {
  26.     string Chars = @"~`!@#$%^&*()\/',.?;:|";
  27.     var RNG = new Random();
  28.  
  29.     // Select a character to insert into the password
  30.     string Character = new String(Enumerable.Repeat(Chars, 1).Select(s => s[RNG.Next(s.Length)]).ToArray());
  31.  
  32.     // output a 9 character long password to which a single character will be added
  33.     return CreateKey(9).Insert(RNG.Next(9), Character);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement