[英]Max length of base64 encoded salt/password for this algorithm
下面是用於在我正在重寫的應用程序中哈希密碼的代碼片段:
internal string GenerateSalt()
{
byte[] buf = new byte[16];
(new RNGCryptoServiceProvider()).GetBytes(buf);
return Convert.ToBase64String(buf);
}
internal string EncodePassword(string pass, string salt)
{
byte[] bIn = Encoding.Unicode.GetBytes(pass);
byte[] bSalt = Convert.FromBase64String(salt);
byte[] bAll = new byte[bSalt.Length + bIn.Length];
Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
HashAlgorithm s = HashAlgorithm.Create("SHA512");
byte[] bRet = s.ComputeHash(bAll);
return Convert.ToBase64String(bRet);
}
哈希密碼和鹽稍后存儲在數據庫中。 什么是base64編碼的鹽和密碼的最大長度?
由於SHA- 512哈希總是512位= 64字節而base64需要(n + 2 - ((n + 2) % 3)) / 3 * 4
字節,因此總是需要88個字節來存儲數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.