我正在研究一些 C# 安全代码,当我看到它正在使用 HMACSHA1 类时,我正准备替换它。该代码用于散列密码以存储在数据库中。引起我注意的是它使用密码作为 HMAC 密钥,这正是计算哈希的目的。那么将数据用于密钥和您的散列可以吗?这会使安全性更强还是更弱?
伪代码:
string pwd = "My~!Cra2y~P@ssWord1#123$";
using (HMACSHA1 hasher = new HMACSHA1())
{
hasher.Key = encoding.GetBytes(pwd); // using password for the key
return BytesToHex(hasher.ComputeHash(encoding.GetBytes(pwd))); // computing the hash for the password
}