繁体   English   中英

如何在数据库中存储盐化的哈希密码

[英]How to store salted hashed password in the database

我试图找出一种方法来将加盐的哈希密码存储到我的数据库中。 这是我第一次这样做,所以我不确定该如何进行。

我的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 )页面或数据层中,该放在哪里? 然后,你们都怎么建议我将这个哈希密码和其他属性一起放在数据库中? 我应该将其直接存储为哈希值,还是先以纯文本格式存储,然后将其更新为哈希值。 谢谢!

我建议您使用现有的实现,而不要在这里滚动自己的实现。

asp.net成员数据库非常适合此用途 ,并且开箱即用地使用了盐化和哈希处理

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM