简体   繁体   English

使用“$2y$”标识符散列密码

[英]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.我有上面这样的代码,它们工作正常但我要把它们用于 moodle 项目,所以我需要 hash 我的密码和“2y$”标识符。

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)我不能像 cryptsharpofficial 那样使用 nuGetPackages,因为当我想在 Windows 11 月 10 日更新 (10586) 中使用它时它会出错

I just installed "BCrypt.Net-Next" and codes shown in below works well: 我刚刚安装了“ BCrypt.Net-Next”,下面显示的代码运行良好:

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

Thanks to @iainn, his comment link: Hashing Password with "$2y$" identifier 感谢@iainn,他的评论链接: 带有“ $ 2y $”标识符的哈希密码

The BCrypt.Net-Next by default uses the $2a$ algorithm. BCrypt.Net-Next默认使用$2a$算法。

You can have it explicitly use $2y$ by specifying the bcryptMinorRevision of the GenerateSalt() function like this:您可以通过指定GenerateSalt() function 的bcryptMinorRevision来明确使用$2y$ ,如下所示:

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:以下是该库中反编译的GenerateSalt() function 的前几行:

        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");
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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