MD5哈希加盐保持密码数据库在C#加盐、密码、数据库

2023-09-02 01:24:58 作者:捧猫少女

你能告诉我一些简单的算法,通过MD5哈希的用户密码,但与盐作为提高可靠性。

现在我有这个:

 私有静态字符串GenerateHash(字符串值)
{
    VAR数据= System.Text.Encoding.ASCII.GetBytes(值);
    数据= System.Security.Cryptography.MD5.Create()ComputeHash(数据)。
    返回Convert.ToBase64String(数据);
}
 

解决方案 MD5加密数据 升级加盐的使用 md5有点咸 保护数据安全 详解

您可以使用的 HMACMD5 类:

  VAR HMACMD5 =新HMACMD5(盐);
变种saltedHash = hmacMD5.ComputeHash(密码);
 

工程与SHA-1,SHA256,SHA384,SHA512和RIPEMD160还有:

  VAR HMACSHA1 =新HMACSHA1(盐);
变种saltedHash = hmacSHA1.ComputeHash(密码);
 

两个密码预计为字节数组。

如果您有字符串你必须将它们转换为字节第一:

  VAR盐= System.Text.Encoding.UTF8.GetBytes(我的盐);
VAR密码= System.Text.Encoding.UTF8.GetBytes(我的密码);
 

Could you please advise me some easy algorithm for hashing user password by MD5, but with salt for increasing reliability.

Now I have this one:

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);
}

解决方案

You can use the HMACMD5 class:

var hmacMD5 = new HMACMD5(salt);
var saltedHash = hmacMD5.ComputeHash(password);

Works with SHA-1, SHA256, SHA384, SHA512 and RIPEMD160 as well:

var hmacSHA1 = new HMACSHA1(salt);
var saltedHash = hmacSHA1.ComputeHash(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");