C# 如何使用 Salt 创建 SHA256 哈希?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14112440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a SHA256 Hash with Salt?
提问by David
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 can I call this function.
我目前正在开发一个 Visual Studio C# windows 窗体项目。但是,我对 SHA256 + salted 的工作方式感到困惑。我在网上找到了一些示例,但无法理解如何调用此函数。
I would like to call this function in a login form connecting to a database (Microsoft Access 2010).
我想在连接到数据库(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; }
- 如何通过单击按钮并从 读取密码来调用此函数
Textbox
? - 如何在
Messagebox.Show
方法中显示哈希值 ?(为了我的测试目的) 是否可以比较两个文本(散列和加盐)并给出肯定的结果?
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; }
How do I create an SHA256 hash with salt?
如何使用盐创建 SHA256 哈希?
shavalue = (sha256encrypt("password", "username");
saltedandhashtext = CreateSalt(shavalue);
采纳答案by jAC
For the first question look at CC Inc's answer.
对于第一个问题,请查看 CC Inc 的回答。
To the second point:
MessageBox.Show(sha256encrypt(textBox1.Text, "SampleUserName"));
关于第二点:
MessageBox.Show(sha256encrypt(textBox1.Text, "SampleUserName"));
3) Yes, it is.
3) 是的,是的。
You can compare two strings with the ==
comparator or string.Equals()
.
您可以使用比较==
器或比较两个字符串string.Equals()
。
public bool compareHashs(string hash1, string hash2){
if(hash1.Equals(hash2) //or hash1 == hash2
return true;
}else{
return false;
}
}
回答by CC Inc
What you would do is, on the click of the button, pass the textbox value and username to the sha256encrypt
function, for example:
您要做的是,单击按钮,将文本框值和用户名传递给sha256encrypt
函数,例如:
private void button1_Click(object sender, EventArgs e)
{
sha256encrypt(textBox1.Text, "SampleUserName");
}
For the second question, do the same but with Messagebox.Show:
对于第二个问题,使用 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"));