我正在使用C#和BCrypt.Net哈希密码。
例如:
string salt = BCrypt.Net.BCrypt.GenerateSalt(6);
var hashedPassword = BCrypt.Net.BCrypt.HashPassword("password", salt);
//This evaluates to True. How? I'm not telling it the salt anywhere, nor
//is it a member of a BCrypt instance because there IS NO BCRYPT INSTANCE.
Console.WriteLine(BCrypt.Net.BCrypt.Verify("password", hashedPassword));
Console.WriteLine(hashedPassword);
如果BCrypt没有将盐保存在任何地方,那么BCrypt如何使用哈希验证密码。我唯一的想法是,它以某种方式在哈希的末尾添加了盐。
这是正确的假设吗?
最佳答案
How is BCrypt verifying the password with the hash if it's not saving the salt anywhere?
显然,它没有做任何这样的事情。盐必须保存在某个地方。
让我们在Wikipedia上查找密码加密方案。从http://en.wikipedia.org/wiki/Crypt_(Unix):
The output of the function is not merely the hash: it is a text string which also encodes the salt and identifies the hash algorithm used.
另外,您对此主题的previous question的回答包括指向source code的链接。源代码的相关部分是:
StringBuilder rs = new StringBuilder();
rs.Append("$2");
if (minor >= 'a') {
rs.Append(minor);
}
rs.Append('$');
if (rounds < 10) {
rs.Append('0');
}
rs.Append(rounds);
rs.Append('$');
rs.Append(EncodeBase64(saltBytes, saltBytes.Length));
rs.Append(EncodeBase64(hashed,(bf_crypt_ciphertext.Length * 4) - 1));
return rs.ToString();
显然,返回的字符串是版本信息,其次是使用的轮数,其次是编码为base64的salt,然后是编码为base64的哈希。
关于c# - 有人可以解释BCrypt如何验证哈希吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5393803/