我有一个简单的密码加密器,当用户注册时,它会给我一个散列/加盐密码存储在我的数据库中。代码:
public static string GenerateHashWithSalt(string enteredPassword, string enteredSalt)
{
string sHashWithSalt = enteredPassword + enteredSalt;
byte[] saltedHashBytes = Encoding.UTF8.GetBytes(sHashWithSalt);
System.Security.Cryptography.HashAlgorithm algorithm = new System.Security.Cryptography.SHA256Managed();
byte[] hash = algorithm.ComputeHash(saltedHashBytes);
return Convert.ToBase64String(hash);
}
当用户登录时,我认为我不能简单地通过这段代码将输入的密码放回原处并进行比较,因为它会给我不同的结果。我怎样才能简单地将存储的密码与输入的登录密码进行比较?
最佳答案
创建帐户时,您将有一个 password hash
列,该列将由 GenerateHashWithSalt(password, salt);
填充,其中密码由他们提供然后随机生成盐。然后盐将与密码哈希一起存储。
然后,当您需要查看用户名/密码是否有效时,您可以使用 storedpassword == GenerateHashWithSalt(providedPassword, saltFromDb)
或类似的方法。如果结果相同,那么您就知道他们输入了正确的密码
关于c# - 登录时比较散列和加盐密码的加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15120996/