Skip to content

Instantly share code, notes, and snippets.

@vpetkovic
Created September 24, 2019 13:05
Show Gist options
  • Save vpetkovic/59086607ad038f88e17e5e2974a1e4d8 to your computer and use it in GitHub Desktop.
Save vpetkovic/59086607ad038f88e17e5e2974a1e4d8 to your computer and use it in GitHub Desktop.
Helper classes
public void GenerateNewPassword(PasswordOptions opts = null)
{
#region Generating New Password
if (opts == null) opts = new PasswordOptions()
{
RequiredLength = 12,
RequiredUniqueChars = 4,
RequireDigit = true,
RequireLowercase = true,
RequireNonAlphanumeric = true,
RequireUppercase = true
};
string[] randomChars = new[] {
"ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase
"abcdefghijkmnopqrstuvwxyz", // lowercase
"0123456789", // digits
"!@$?_-" // non-alphanumeric
};
random = new Random(Environment.TickCount);
List<char> chars = new List<char>();
if (opts.RequireUppercase)
chars.Insert(random.Next(0, chars.Count),
randomChars[0][random.Next(0, randomChars[0].Length)]);
if (opts.RequireLowercase)
chars.Insert(random.Next(0, chars.Count),
randomChars[1][random.Next(0, randomChars[1].Length)]);
if (opts.RequireDigit)
chars.Insert(random.Next(0, chars.Count),
randomChars[2][random.Next(0, randomChars[2].Length)]);
if (opts.RequireNonAlphanumeric)
chars.Insert(random.Next(0, chars.Count),
randomChars[3][random.Next(0, randomChars[3].Length)]);
for (int i = chars.Count; i < opts.RequiredLength
|| chars.Distinct().Count() < opts.RequiredUniqueChars; i++)
{
string rcs = randomChars[random.Next(0, randomChars.Length)];
chars.Insert(random.Next(0, chars.Count),
rcs[random.Next(0, rcs.Length)]);
}
#endregion
nh_api_password = new string(chars.ToArray());
}
private void GenerateNewSecret(int lenght = 65)
{
const string chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
nh_api_secret = new string(Enumerable.Repeat(chars, lenght)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
public void GenerateSHA512Hash(string password)
{
HashAlgorithm alg = new SHA512Managed();
var HashedBytes = Encoding.UTF8.GetBytes(password);
nh_api_SHA_hash = Convert.ToBase64String(alg.ComputeHash(HashedBytes));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment