Skip to content

Instantly share code, notes, and snippets.

@BryanWilhite
Created September 22, 2016 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BryanWilhite/efa3a1e72902ae2f23d19c681cd99f15 to your computer and use it in GitHub Desktop.
Save BryanWilhite/efa3a1e72902ae2f23d19c681cd99f15 to your computer and use it in GitHub Desktop.
using System;
namespace Cetera.Security
{
/// <summary>
/// Password utilities.
/// </summary>
public static class PasswordUtility
{
/// <summary>
/// Generates the random password.
/// </summary>
/// <param name="passwordLength">Length of the password.</param>
/// <param name="seed">The seed.</param>
/// <returns></returns>
/// <remarks>
/// For more details, see “Generate random password in C#” by Mads Kristensen
/// [http://madskristensen.net/post/Generate-random-password-in-C]
/// </remarks>
public static string GenerateRandomPassword(int passwordLength, int seed)
{
var allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
var chars = new char[passwordLength];
var r = new Random(seed);
for (int i = 0; i < passwordLength; i++)
{
chars[i] = allowedChars[r.Next(0, allowedChars.Length)];
}
return new string(chars);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment