为什么不密码存储在与SHA1数据库中,然后使用HMAC与客户端指定的键为沟通?
让服务器生成一个随机密钥并通过登录请求发送它。客户端计算密码的SHA1哈希值,然后使用服务器指定的密钥计算HMAC SHA1哈希值。然后服务器验证结果是否正确。
在客户端:
// password is the plaintext password
// keyb64 is a random key specified by the server, encoded in base64.
string ComputeSecureHash(string password, string keyb64)
{
byte[] data = Encoding.UTF8.GetBytes(password);
byte[] key = Convert.FromBase64String(keyb64);
byte[] hash;
byte[] machash;
// compute a plain SHA1 hash of the specified data
using (SHA1Managed sha1 = new SHA1Managed())
{
hash = sha1.ComputeHash(data);
}
// now compute a HMAC hash of that hash, using the random key.
using (HMACSHA1 sha1mac = new HMACSHA1(key))
{
machash = sha1mac.ComputeHash(hash);
}
return Convert.ToBase64String(machash);
}
在服务器端:
// hash is the string produced by the function above
// realHash is the SHA1 hash of the real password, which you've fetched from the db
// key is the key you generated for this login session
bool VerifyHash(string hash, byte[] realHash, byte[] key)
{
byte[] machash;
using (HMACSHA1 sha1mac = new HMACSHA1(key))
{
machash = sha1mac.ComputeHash(realHash);
}
return (Convert.ToBase64String(machash) == hash);
}
这可以让你无需破解密码认证通过明文媒体。
谢谢 - 你有什么建议如何处理这些问题? – Aaron
首先选择简单的测试数据,开始工作并进入更复杂的情况。例如:最初选择全部为0的数据,CBC,只有一个块大小的数据,没有填充。在工作的时候开始增加复杂度。无论如何,如果你想要真正的安全性,你需要由经验丰富的专业人员进行代码审查,我确实需要。 安全性不容易,加密部分是容易的部分。 或使用SSL。哦,如果他们有帮助,接受答案。 – zaph