Can someone explain how BCrypt verifies a hash?

后端 未结 2 709
萌比男神i
萌比男神i 2020-11-28 07:39

I\'m using C# and BCrypt.Net to hash my passwords.

For example:

string salt = BCrypt.Net.BCrypt.GenerateSalt(6);
var hashedPassword = BCrypt.Net.BCry         


        
2条回答
  •  春和景丽
    2020-11-28 08:07

    How is BCrypt verifying the password with the hash if it's not saving the salt anywhere?

    Clearly it is not doing any such thing. The salt has to be saved somewhere.

    Let's look up password encryption schemes on Wikipedia. From 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.

    Alternatively, an answer to your previous question on this subject included a link to the source code. The relevant section of the source code is:

        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();
    

    Clearly the returned string is version information, followed by the number of rounds used, followed by the salt encoded as base64, followed by the hash encoded as base64.

提交回复
热议问题