SQL Server HASHBYTES SHA2_512和C# [英] SQL Server HASHBYTES SHA2_512 and C#
本文介绍了SQL Server HASHBYTES SHA2_512和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在SQL Server中运行以下命令:
If I run the following in SQL Server:
SELECT HASHBYTES('SHA2_512', 'THE CAT SAT ON THE MAT')
然后在C#中运行它
string test = WordFunctions.SHA512("THE CAT SAT ON THE MAT");
我两者都获得了相同的价值,这很棒.但是,如果我在C#代码中传递一个字符串变量,则如下所示:
I get the same value back for both which is great. However, if I pass a string variable in my C# code so something like this:
string words = "THE CAT SAT ON THE MAT"
byte[] test
test = WordFunctions.SHA512(words);
那我得到的价值不一样吗?!
then I don't get the same value?!
有什么想法吗?
SHA512函数如下所示:
the SHA512 function looks like this:
public static byte[] SHA512(String plaintext)
{
// convert the passPhrase string into a byte array
ASCIIEncoding AE = new ASCIIEncoding();
byte[] passBuff = AE.GetBytes(plaintext);
SHA512Managed hashVal = new SHA512Managed();
byte[] passHash = hashVal.ComputeHash(passBuff);
return passHash;
}
推荐答案
以下代码对我有用:
private static readonly Encoding Encoding1252 = Encoding.GetEncoding(1252);
public static byte[] SHA1HashValue(string s)
{
byte[] bytes = Encoding1252.GetBytes(s);
var sha1 = SHA512.Create();
byte[] hashBytes = sha1.ComputeHash(bytes);
return hashBytes;
}
这篇关于SQL Server HASHBYTES SHA2_512和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文