c# - 有人可以解释BCrypt如何验证哈希吗?

标签 c# bcrypt bcrypt.net

我正在使用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/

相关文章:

node.js - 使用 bcrypt 来确保密码没有重复

c++ - cout 和 sleep 导致不同的结果

c# - jBCrypt 0.3 C# 端口 (BCrypt.net)

c# - 在 C# ASP.NET 中添加自定义 hashAlgorithmType

bcrypt 的 .net 实现

c# - 在 ASP.NET MVC 中将属性类型设置为另一个 ViewModel 的 ViewModel 是一种不好的做法吗

c# - 为什么从 C# 启动时从 VB 调用 C# COM 对象会出现 0x80070002 错误?

c# - 需要比 XML 更简单的数据源

c# - 获取串口连接的设备名称

ruby-on-rails-3 - 在 Windows 上使用带有 ruby​​2.0 的 bcrypt 3.0.1 的问题