简体   繁体   English

如何对用户密码进行哈希处理?

[英]How to hash users passwords?

please do not mark this as a duplicate because I've already checked multiple posts but none of them helped me. 请不要将其标记为重复项,因为我已经检查了多个帖子,但是它们都没有帮助我。

Asp.net MVC - How to hash password Asp.net MVC-如何对密码进行哈希处理

Hash and salt passwords in C# C#中的哈希和盐密码

I'm building a website that has a login page, and I read this post 我正在建立一个具有登录页面的网站,并且阅读了这篇文章

https://www.codeproject.com/Articles/704865/Salted-Password-Hashing-Doing-it-Right#normalhashing https://www.codeproject.com/Articles/704865/Salted-Password-Hashing-Doing-it-Right#normalhashing

on how to make my login secure. 有关如何确保我的登录安全的信息。

Now I understand that on each sign up I have to create CSPRNGs(Cryptographically secure pseudorandom number generator) and add it to the password submitted by the user, then hash this mix and store it in the DB, and store the salt for each user because it's a random one for each user. 现在我了解到,在每次注册时,我必须创建CSPRNGs(加密安全的伪随机数生成器)并将其添加到用户提交的密码中,然后对该混合进行哈希处理并将其存储在数据库中,并为每个用户存储盐,因为每个用户都是随机的。 My question is what is the easiest and the most simple way to do that. 我的问题是什么是最简单,最简单的方法。

In my controller I'm taking the username and the password via this method 在我的控制器中,我通过这种方法获取用户名和密码

public JsonResult Login(LoginModel data)
{  
   //some code...
   //...
}

My login model contain 我的登录模型包含

public class LoginModel
{
    public string Username { get; set; }
    public string Password { get; set; }
}

so I'm getting the username and password like (string a = data.Username, ...) 所以我得到的用户名和密码像(字符串a = data.Username,...)

I already added this to my project but I don't know how to continue from here 我已经将其添加到我的项目中,但是我不知道如何从这里继续

using System.Security.Cryptography;

I know how to save in the DB for sure I just want to know, how to make a random salt (maybe using RNGCryptoServiceProvider), and how to add it to the user's password, and then hash the final result. 我肯定知道如何保存在数据库中,如何随机分配盐(可能使用RNGCryptoServiceProvider),以及如何将其添加到用户密码中,然后对最终结果进行哈希处理。 Thanks for any help. 谢谢你的帮助。

The answer to every "How do I securely hash passwords" question is the same: YOU DON'T . 每个“如何安全地对密码进行散列”问题的答案都是相同的: 不知道 You're in the business of making unique websites, not in the security business. 您从事的是制作独特网站的业务,而不是安全业务。 You don't know what you're doing, and you won't recognize if you're doing something wrong. 您不知道自己在做什么,也不会知道自己在做错什么。 Until your database leaks out in one way or another, and then you're too late to do anything about it, compromising your users' security. 直到您的数据库以一种或另一种方式泄漏出去,然后您就为时已晚,对此采取任何措施,从而损害了用户的安全。

You use well-researched, battle-hardened, maintained code to store user logins. 您可以使用经过充分研究,经过严格保护的维护代码来存储用户登录名。 When you're on ASP.NET MVC, you use ASP.NET Identity . 使用ASP.NET MVC时,可以使用ASP.NET Identity You DO NOT roll your own. 你不要自己滚。 Period. 期。

You could go about adding salt and hashing the password by using a method such as this. 您可以使用诸如此类的方法添加盐和对密码进行哈希处理。 Here we send a method a string. 在这里,我们向方法发送一个字符串。 This is basically how entity framework hash's a password when you create a user using the login section. 从本质上讲,这就是当您使用登录部分创建用户时,实体框架散列密码的方式。

    public static string EncodePassword(string password)
    {
        byte[] salt;
        byte[] buffer2;
        if (password == null)
        {
            throw new ArgumentNullException("password");
        }
        using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, 0x10, 0x3e8))
        {
            salt = bytes.Salt;
            buffer2 = bytes.GetBytes(0x20);
        }
        byte[] dst = new byte[0x31];
        Buffer.BlockCopy(salt, 0, dst, 1, 0x10);
        Buffer.BlockCopy(buffer2, 0, dst, 0x11, 0x20);
        return Convert.ToBase64String(dst);
    }//Entity Framework default way 

The above uses using System.Security.Cryptography; 上面的用法using System.Security.Cryptography; and system.Buffer system.Buffer

I did this way, and it's working fine. 我是这样做的,并且工作正常。

I added a new class 我增加了一个新班

public class Helper
{

    public String CreateSalt(int size)
    {
        var rng = new RNGCryptoServiceProvider();
        var buff = new byte[size];
        rng.GetBytes(buff);
        return Convert.ToBase64String(buff);
    }
    public String GenerateSha256Hash(string input, string salt)
    {
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
        SHA256Managed shaString = new SHA256Managed();
        byte[] hash = shaString.ComputeHash(bytes);

        return BitConverter.ToString(hash);
    }

}

} }

And in my controller I added an instance of this class 在我的控制器中,我添加了此类的一个实例

Helper myHelper = new Helper();

Then I'm passing values to 然后我将值传递给

string salt = myHelper.CreateSalt(5);
string hashedPassword = myHelper.GenerateSha256Hash(*PasswordField*, salt);
hashedPassword = hashedPassword.Replace("-", string.Empty).Substring(0, 16);

I'm removing the "-"from the hashed password and then I'm taking the first 16 characters. 我要从哈希密码中删除“-”,然后使用前16个字符。

Thanks to @Luke Joshua Park, @gusto2 , @JamesS and @CodeCaster for their help. 感谢@Luke Joshua Park,@ gusto2,@ JamesS和@CodeCaster的帮助。

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

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