如何创建带盐的SHA256哈希?

8

我目前正在开发一个基于Visual Studio C#的Windows表单项目。然而,我对SHA256 + salted的工作原理感到困惑。我在网上找到了一些示例,但无法理解如何调用此函数。

我希望在连接到数据库(Microsoft Access 2010)的登录表单中调用此函数。

  • How do I call this function by a click of a button and reading the password from a Textbox?
  • How do i display out the hash value in a Messagebox.Show method? (For my testing purpose)
  • Is it possible to compare two text (hashed and salted) and giving a positive result?

    public static string sha256encrypt(string phrase, string UserName)
    {
        string salt = CreateSalt(UserName);
        string saltAndPwd = String.Concat(phrase, salt);
        UTF8Encoding encoder = new UTF8Encoding();
        SHA256Managed sha256hasher = new SHA256Managed();
        byte[] hashedDataBytes =      sha256hasher.ComputeHash(encoder.GetBytes(saltAndPwd));
        string hashedPwd = String.Concat(byteArrayToString(hashedDataBytes), salt);
        return hashedPwd;
    }
    
    public static string byteArrayToString(byte[] inputArray)
    {
        StringBuilder output = new StringBuilder("");
        for (int i = 0; i < inputArray.Length; i++)
        {
            output.Append(inputArray[i].ToString("X2"));
        }
        return output.ToString();
    }
    
    private static string CreateSalt(string UserName)
    {
        string username = UserName;
        byte[] userBytes; 
        string salt;
        userBytes = ASCIIEncoding.ASCII.GetBytes(username);
        long XORED = 0x00; 
    
        foreach (int x in userBytes)
            XORED = XORED ^ x;
    
        Random rand = new Random(Convert.ToInt32(XORED));
        salt = rand.Next().ToString();
        salt += rand.Next().ToString();
        salt += rand.Next().ToString();
        salt += rand.Next().ToString();
        return salt;
    }
    

如何使用盐值创建SHA256哈希?

shavalue = (sha256encrypt("password", "username");
saltedandhashtext = CreateSalt(shavalue);

4
若未经阅读和理解,我不确定是否会信任这段代码,因为它将SHA-256称作“加密”,并且只使用了最多256个不同的盐值... - Matti Virkkunen
5
不要浪费时间在让这段代码运行上。扔掉它,从头开始写新的代码。特别是使用 Rfc2898DeriveBytes,它使用一个拉伸哈希函数并可以自动创建一个盐值。 - CodesInChaos
2
由于您正在哈希密码,因此应考虑使用诸如PBKDF2之类的缓慢密钥派生函数。在CSharp中,可以使用Rfc2898DeriveBytes类来实现,正如CodesInChaos已经提到的那样。 - martinstoeckli
@CodesInChaos...请注意 .Net 7: SYSLIB0041:某些Rfc2898DeriveBytes构造函数已过时。请参阅 https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/syslib0041 我今天(2023年4月11日)才发现这一点。 - John D
3个回答

4
您需要做的是,在按钮点击时,将文本框的值和用户名传递给sha256encrypt函数,例如:
    private void button1_Click(object sender, EventArgs e)
    {
        sha256encrypt(textBox1.Text, "SampleUserName");
    }

对于第二个问题,使用Messagebox.Show执行相同的操作:

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(sha256encrypt(textBox1.Text, "SampleUserName"));
    }

第三点:我不确定你的意思是什么,但如果你想要对一个文本进行盐值处理并将其与哈希文本进行比较:
if(sha256encrypt("password", "username") == CreateSalt("password"))
   return true;
else
   return false;

或者如果你想手动比较它们:

MessageBox.Show(sha256encrypt("password", "username") + "\n\r" + CreateSalt("password"));

@JanesAbouChleih 你是不是想在实际问题上发帖? - CC Inc

2

对于第一个问题,请查看CC公司的回答。

至于第二点: MessageBox.Show(sha256encrypt(textBox1.Text, "SampleUserName"));

3)是的,可以。 您可以使用==比较器或string.Equals()函数来比较两个字符串。

public bool compareHashs(string hash1, string hash2){
   if(hash1.Equals(hash2) //or hash1 == hash2
      return true;
   }else{
      return false;
   }  
}

0
public string ComputeSHA256Hash(string rawData)
{
    // Create a SHA256   
    using (SHA256 sha256Hash = SHA256.Create())
    {
        // ComputeHash - returns byte array  
        byte[] bytes = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
        // Convert byte array to a string   
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < bytes.Length; i++)
        {
            builder.Append(bytes[i].ToString("x2"));
        }
        return builder.ToString();
    }
}

这似乎是现在正确的方法。Sha256Managed已被弃用,他们现在建议使用SHA256.Create()方法。 - Zonus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接