C# Windows Phone 8.1应用程序和UWP应用程序之间的SHA1哈希不相同
在Windows Phone 8中,我可以执行以下操作:C# Windows Phone 8.1应用程序和UWP应用程序之间的SHA1哈希不相同,c#,uwp,sha1,windows-10-universal,C#,Uwp,Sha1,Windows 10 Universal,在Windows Phone 8中,我可以执行以下操作: SHA1Managed s = new SHA1Managed(); UTF8Encoding enc = new UTF8Encoding(); s.ComputeHash(enc.GetBytes(password.ToCharArray())); string hash = BitConverter.ToString(s.Hash).Replace("-", "").ToLower(); 对于UWP应用程序,我正在执行:
SHA1Managed s = new SHA1Managed();
UTF8Encoding enc = new UTF8Encoding();
s.ComputeHash(enc.GetBytes(password.ToCharArray()));
string hash = BitConverter.ToString(s.Hash).Replace("-", "").ToLower();
对于UWP应用程序,我正在执行:
IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(password, BinaryStringEncoding.Utf8);
HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);
if (buffHash.Length != objAlgProv.HashLength)
{
throw new Exception("There was an error creating the hash");
}
string strHashBase64 = CryptographicBuffer.EncodeToBase64String(buffHash);
string hash = strHashBase64.Replace("-", "").ToLower();
我没有得到同样的结果
例如,如果我有文本“Windows8”,我会得到
6517856f8c3a3fda3ae28305a05d127f0e1bdb97
(Windows Phone 8.1)
及
zrefb4w6p9o64omfof0sfw4b25c=
(UWP)
我不太清楚我做错了什么。目标是只获取字符串的SHA1哈希。而不是在UWP代码(
CryptographicBuffer.EncodeToBase64String(buffHash)
)下编码到base64,您可能希望使用以下代码编码到十六进制:
string strHex = CryptographicBuffer.EncodeToHexString(buffHash);
string hash = strHex.ToLower();
因此,您的Windows Phone 8代码和UWP代码都将产生相同的输出(十六进制编码的SHA1)。将
EncodeToBase64String
替换为EncodeToHexString
,然后您将得到相同的结果。