Skip to content

Instantly share code, notes, and snippets.

@ibrahimatay
Created December 6, 2018 10:17
Show Gist options
  • Select an option

  • Save ibrahimatay/262f44c3c4d168b998490b5bbebda0cc to your computer and use it in GitHub Desktop.

Select an option

Save ibrahimatay/262f44c3c4d168b998490b5bbebda0cc to your computer and use it in GitHub Desktop.
public static string RandomString(int length)
{
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
byte[] data = new byte[length];
using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
{
crypto.GetBytes(data);
}
StringBuilder result = new StringBuilder(length);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length)]);
}
return result.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment