[英]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();
我有上面這樣的代碼,它們工作正常但我要把它們用於 moodle 項目,所以我需要 hash 我的密碼和“2y$”標識符。
我能用什么? 我不能像 cryptsharpofficial 那樣使用 nuGetPackages,因為當我想在 Windows 11 月 10 日更新 (10586) 中使用它時它會出錯
我剛剛安裝了“ BCrypt.Net-Next”,下面顯示的代碼運行良好:
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(passToHash);
感謝@iainn,他的評論鏈接: 帶有“ $ 2y $”標識符的哈希密碼
BCrypt.Net-Next
默認使用$2a$
算法。
您可以通過指定GenerateSalt()
function 的bcryptMinorRevision
來明確使用$2y$
,如下所示:
string salt = BCrypt.Net.BCrypt.GenerateSalt(8, 'y');
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(passToHash, salt);
以下是該庫中反編譯的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.