To generate a password, I generate a key using RNGCryptoServiceProvider. The key is 9 alphanumeric characters long into which I insert a single non-alphanumeric character for a full length of 10 characters.
Having a mix of Uppercase, lowercase, numbers and symbols and being 10 characters long, as a plain text password (which you'd enter when logging in somewhere), this password should be more than adequate for most situations.
// used for more than just generating a password
public static string CreateKey(int length)
{
string Chars = "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(Chars[(int)(N % (uint)Chars.Length)]);
}
}
return Sb.ToString();
}
public static string CreatePasswordString()
{
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());
return CreateKey(9).Insert(RNG.Next(9), Character);
}
This generates a password which is then hashed in my application using bcrypt as per below:
public static string CreatePassword(string password)
{
byte[] PasswordBytes = Encoding.ASCII.GetBytes(password);
return Crypter.Blowfish.Crypt(PasswordBytes);
}
I can then validate the input at login more or less with this function - I haven't tested this code since I'm writing it for the first time for this question.
public static bool ValidatePassword(string emailAddress, string password)
{
var User = db.Users.FirstOrDefault(x => x.EmailAddress == emailAddress);
if (User == null) return false;
return Crypter.Blowfish.CheckPassword(password, User.Password);
}
I used to use PBKDF2 but I was informed that bcrypt is better so here we are. While I understand the rule "never use your own auth", is this code likely to provide decent security?
What sort of vulnerabilities are present in implementing this system for security?
While I understand the rule "never use your own auth"
Why did you do it anyway? \$\endgroup\$