带有盐的 MD5 哈希值,用于在 C# 中将密码保存在 DB 中 [英] MD5 hash with salt for keeping password in DB in C#
本文介绍了带有盐的 MD5 哈希值,用于在 C# 中将密码保存在 DB 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你能告诉我一些简单的算法来通过 MD5 散列用户密码,但使用 salt 来提高可靠性.
Could you please advise me some easy algorithm for hashing user password by MD5, but with salt for increasing reliability.
现在我有了这个:
private static string GenerateHash(string value)
{
var data = System.Text.Encoding.ASCII.GetBytes(value);
data = System.Security.Cryptography.MD5.Create().ComputeHash(data);
return Convert.ToBase64String(data);
}
推荐答案
您可以使用 HMACMD5 类:
var hmacMD5 = new HMACMD5(salt);
var saltedHash = hmacMD5.ComputeHash(password);
也适用于 SHA-1、SHA256、SHA384、SHA512 和 RIPEMD160:
Works with SHA-1, SHA256, SHA384, SHA512 and RIPEMD160 as well:
var hmacSHA1 = new HMACSHA1(salt);
var saltedHash = hmacSHA1.ComputeHash(password);
salt
和 password
都是字节数组.
Both salt
and password
are expected as byte arrays.
如果您有字符串,则必须先将它们转换为字节:
If you have strings you'll have to convert them to bytes first:
var salt = System.Text.Encoding.UTF8.GetBytes("my salt");
var password = System.Text.Encoding.UTF8.GetBytes("my password");
这篇关于带有盐的 MD5 哈希值,用于在 C# 中将密码保存在 DB 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文