简体   繁体   中英

Hashing Password with "$2y$" identifier

String strAlgName = HashAlgorithmNames.Sha1;

HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(strAlgName);
CryptographicHash objHash = objAlgProv.CreateHash();

String strMsg1 = "test";
IBuffer buffMsg1 = CryptographicBuffer.ConvertStringToBinary(strMsg1, BinaryStringEncoding.Utf16BE);

objHash.Append(buffMsg1);

IBuffer buffHash1 = objHash.GetValueAndReset();

I have codes like this above, they are working fine but I'm gonna use them for moodle project, so I need to hash my passwords with "2y$" identifier.

What can I use? I can't use nuGetPackages like cryptsharpofficial, cause it gives error when I want to use it in Windows 10 November Update (10586)

I just installed "BCrypt.Net-Next" and codes shown in below works well:

string hashedPassword = BCrypt.Net.BCrypt.HashPassword(passToHash);

Thanks to @iainn, his comment link: Hashing Password with "$2y$" identifier

The BCrypt.Net-Next by default uses the $2a$ algorithm.

You can have it explicitly use $2y$ by specifying the bcryptMinorRevision of the GenerateSalt() function like this:

string salt = BCrypt.Net.BCrypt.GenerateSalt(8, 'y');
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(passToHash, salt);

Here are the first few lines of the decompiled GenerateSalt() function from that library:

        public static string GenerateSalt(int workFactor, char bcryptMinorRevision = 'a')
        {
            if (workFactor < 4 || workFactor > 31)
            {
                throw new ArgumentOutOfRangeException("workFactor", workFactor, $"The work factor must be between {(short)4} and {(short)31} (inclusive)");
            }

            if (bcryptMinorRevision != 'a' && bcryptMinorRevision != 'b' && bcryptMinorRevision != 'x' && bcryptMinorRevision != 'y')
            {
                throw new ArgumentException("BCrypt Revision should be a, b, x or y", "bcryptMinorRevision");
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM