Hash Password in C#? Bcrypt/PBKDF2

前端 未结 9 1025
傲寒
傲寒 2020-12-22 20:06

I looked up msdn and other resources on how to do this but i came up with no clear solutions. This is the best i found http://blogs.msdn.com/b/shawnfa/archive/2004/04/14/gen

9条回答
  •  北海茫月
    2020-12-22 20:50

    It took me forever (days it took days) to find what to actually code to get hashed passwords to work!! so I put it here for convenience.

    You do need to read the documentation and theory1 theory2 and then some or you could be open to security loopholes. Security is a very big topic! Buyer Beware!

    Add the NuGet Package BCrypt.Net to the solution

    const int WorkFactor = 14;
    var HashedPassword = BCrypt.Net.BCrypt.HashPassword(Password, WorkFactor); 
    

    You should adjust the WorkFactor to what is appropriate see discussions. Its a log2 function

    "The number is log2, so every time computers double in speed, add 1 to the default number."

    Then you store the hashed password in your db as passwordFromLocalDB and to test an incoming password like this:

    if (BCrypt.Net.BCrypt.Verify(password, passwordFromLocalDB) == true)
    

    Good Luck!

提交回复
热议问题