如何在数据库中存储盐渍散列密码
问题描述:
我试图找出一种方法将盐渍散列的密码存储到我的数据库中。这是我第一次这样做,所以我不确定这个过程应该如何进行。如何在数据库中存储盐渍散列密码
我的ASP.NET C#Web应用程序首先需要用户注册一个帐户。我有一个数据层,用于调用数据层中方法的业务层,以及用于显示表单控件的表示层。目前我使用纯文本将密码存储在数据库中。我们在数据层中调用createAccount()
方法。用户名,密码和其他属性传递到createAccount()
方法来调用SQL查询来创建Account
记录。用户名和密码由用户在注册页面中指定。我发现,给我提供了一个方法生成与哈希密码如下网站:
public static string GenerateHashWithSalt(string password, string salt)
{
// merge password and salt together
string sHashWithSalt = password + salt;
// convert this merged value to a byte array
byte[] saltedHashBytes = Encoding.UTF8.GetBytes(sHashWithSalt);
// use hash algorithm to compute the hash
System.Security.Cryptography.HashAlgorithm algorithm = new System.Security.Cryptography.SHA256Managed();
// convert merged bytes to a hash as byte array
byte[] hash = algorithm.ComputeHash(saltedHashBytes);
// return the has as a base 64 encoded string
return Convert.ToBase64String(hash);
}
我应该在哪里把这个,在注册页面,在后台代码(.cs
)页面,或在数据层?然后,你怎么会建议我把这个哈希密码和我的其他属性放在我的数据库中?我应该直接将它存储为散列值,或者先将它存储为纯文本,然后将其更新为散列值。谢谢!
将salt存储为纯文本,并将散列存储为字节或base64编码文本。根本不要存储密码。 – 2015-01-31 18:18:25
请参阅https://crackstation.net/hashing-security.htm。除了@ AndrewBarber的评论之外,还要确保你在*服务器*上进行哈希处理 - 你把逻辑放在哪里并不重要,因为你确保你不会在客户端做它。 – 2015-01-31 18:22:54
是的,我设法将我的用户名作为salt传递给哈希密码。我将代码放在我的数据层(添加到数据库)和表示层(比较登录输入的密码)上。十分感谢大家! – user3763216 2015-01-31 18:41:21