使用 BCrypt 的 C# 实现之一对密码进行哈希处理并将其存储到 SQL 数据库中。然而,当我返回验证哈希字符串时,BCrypt 生成的哈希值与要比较的数据库中的哈希值不同。盐和因子显然是相同的。
这是我所知道的
$2a$12$vF/1s3MqIzHwnDshyzH/rOYUelofrj4UWv./vzWqk4o2K0uwhix7W 实际上是“Qwerty123”,它存储在初始化为 [nvarchar] (200) 的列中。
当我根据实现使用 BCrypt.Verify() 或 BCrypt.CheckPassword() 时,我会跟踪它直到它进行比较之前,并且它将与前面提到的哈希值进行比较的哈希值是 $2a $12$vF/1s3MqIzHwnDshyzH/rOKVRePZSXFXaIpDv6.IPkbPEoOxZgSEe
如果仔细观察,您会发现盐和因子部分是相同的。知道是什么原因造成的吗?
我正在使用的显式实现可以在这里找到 http://bcrypt.codeplex.com/
最佳答案
测试建议
private void FindWhatsFailing(string password) //password = Whatever you're passing in to verify BCrypt is working
{
const string expectedpassword = "Qwerty123";
if(expectedpassword != password)
{
Debug.WriteLine("My password isn't what I thought it was");
return;
}
string hashed = BCrypt.HashPassword(expectedpassword , BCrypt.GenerateSalt(12));
if(!BCrypt.Verify(expectedpassword , hashed))
{
Debug.WriteLine("Something is wrong with BCrypt");
return;
}
/// ... Test hashing password, compare to hash of expectedpassword, verify password against hash of itself and expectedpassword
Debug.WriteLine("Everything worked, maybe the database storage is off?");
}
如果 Bcrypt.Verify 在此示例中不起作用,我不知道出了什么问题,但我猜 Bcrypt 实际上并不是这里的问题。
关于c# - 给定相同的盐、字符串和因子,BCrypt 生成不同的哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6540870/