如果我在SQL Server中运行以下命令:
SELECT HASHBYTES('SHA2_512', 'THE CAT SAT ON THE MAT')
然后在C#
中运行它string test = WordFunctions.SHA512("THE CAT SAT ON THE MAT");
我得到了同样的价值,两者都很棒。但是,如果我在C#代码中传递一个字符串变量,就像这样:
string words = "THE CAT SAT ON THE MAT"
byte[] test
test = WordFunctions.SHA512(words);
然后我没有得到相同的价值?!
有什么想法吗?
SHA512功能如下所示:
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;
}
答案 0 :(得分:1)
以下代码对我有用:
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;
}