Share via

Question about salt in Bcrypt

banoo 1 Reputation point
2021-06-23T06:44:07.033+00:00

Hello. Good time
I use bcrypt to encrypt passwords in .net mvc(c#)
In sign-up, I use the following code:

 string salt = BCrypt.Net.BCrypt.GenerateSalt(12);
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(enteredPassword, salt);

and At this point, the hashedPassword is stored in the database

The question I have at this stage is whether salt needs to be stored in the database?

I also use the following code in the login:

--hashedPassword is read from the database
bool verify = BCrypt.Net.BCrypt.Verify(password, hashedPassword,false, hashType : HashType.SHA512);


    if (verify)
    {
    }
    else
    {
    }

The next question is whether the verification was done correctly? Should I not use salt at this stage?
I did not use salt in the login

And the last question is whether it is correct to use hashType: HashType.SHA512 and enhancedEntropy: false in the verify function? Are these settings the best settings?

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Developer technologies | ASP.NET Core | Other

2 answers

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,106 Reputation points
    2021-06-24T05:41:18.75+00:00

    Hi @banoo ,

    From a description of bcrypt at Wikipedia: ... The rest of the hash string includes the cost parameter, a 128-bit salt (Radix-64 encoded as 22 characters), and 184 bits of the resulting hash value (Radix-64 encoded as 31 characters). So the salt is automatically included in the output string which means there is no need to add it by yourself.
    When someone tries to authenticate, retrieve the stored cost and salt. Derive a key from the input password, cost and salt. Encrypt the same well-known string. If the generated cipher text matches the stored cipher text, the password is a match.

    Bcrypt operates in a very similar manner to more traditional schemes based on algorithms like PBKDF2. The main difference is its use of a derived key to encrypt known plain text;
    The function HashPassword has prepended the salt to the password hash, so if you store the output of this, you are storing the salt.
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


  2. cheong00 3,491 Reputation points Volunteer Moderator
    2021-06-24T03:22:55.117+00:00

    Actually if you check the source, HashPassword(inputKey) is equal to HashPassword(inputKey, GenerateSalt()), therefore you know you shouldn't need to store it or HashPassword(inputKey) as a function would be useless.

    For both HashPassword() and Verify(), if you do not pass the hashType parameter, it will use DefaultEnhancedHashType as default which is SHA384. Therefore if you try to do Verify() with HashType.SHA512 as parameter it will not match.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.