private static async Task Register(ConnectedClient client) { string registerUsername = client.reader.ReadString(); string registerPassword = client.reader.ReadString(); string registerEmail = client.reader.ReadString(); string isValid = CheckAccountInfoIsValid(registerUsername, registerPassword, registerEmail); if (isValid != "Valid") { client.Write(MessageType.RegisterError, new string[] { isValid }); return; } string registerSalt = ""; var registerHashedPassword = Passwords.Hash(registerPassword, out registerSalt); Databases.Accounts.Users.Add(new User() { Username = registerUsername, PasswordHash = registerHashedPassword, Email = registerEmail, Salt = registerSalt, CreationDate = DateTime.UtcNow, LastLoginDate = DateTime.UtcNow }); await Databases.Accounts.SaveChangesAsync(); OutputLogAddInvoke($"User account {registerUsername} has been created!"); client.Write(MessageType.RegisterConfirmed, new string[] { }); }
public void Validate() { var salt = Passwords.GenerateSalt(); Assert.IsTrue(Passwords.Validate(Passwords.Hash("123", salt), "123", salt)); Assert.IsFalse(Passwords.Validate(Passwords.Hash("a", salt), "A", salt)); Assert.IsTrue(Passwords.Validate(Passwords.Hash("", salt), null, salt)); }
public void Hash() { const string password = "******"; var salt = Passwords.GenerateSalt(); var hash = Passwords.Hash(password, salt); Passwords.Hash(password, salt).AssertEqual(hash); Assert.AreNotEqual(password, hash); Assert.AreNotEqual(salt, hash); hash = Passwords.Hash("", salt); Assert.IsTrue(hash.Length > 0); Passwords.Hash(null, salt).AssertEqual(hash); }