How do I create a SHA256 Hash with Salt?

前端 未结 2 1345
小鲜肉
小鲜肉 2021-02-15 14:00

I am currently working on a visual studio C# windows form project. However, I am confused by how SHA256 + salted works. I found some examples online but unable to understand how

相关标签:
2条回答
  • 2021-02-15 14:38

    For the first question look at CC Inc's answer.

    To the second point: MessageBox.Show(sha256encrypt(textBox1.Text, "SampleUserName"));

    3) Yes, it is.

    You can compare two strings with the == comparator or string.Equals().

    public bool compareHashs(string hash1, string hash2){
       if(hash1.Equals(hash2) //or hash1 == hash2
          return true;
       }else{
          return false;
       }  
    }
    
    0 讨论(0)
  • 2021-02-15 14:58

    What you would do is, on the click of the button, pass the textbox value and username to the sha256encrypt function, for example:

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

    For the second question, do the same but with Messagebox.Show:

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

    Third point: I am not sure exactly what you mean, but if you want to Salt a text and compare it with the Hashed text:

    if(sha256encrypt("password", "username") == CreateSalt("password"))
       return true;
    else
       return false;
    

    Or if you want to compare them manually:

    MessageBox.Show(sha256encrypt("password", "username") + "\n\r" + CreateSalt("password"));
    
    0 讨论(0)
提交回复
热议问题