c# - Windows Phone 8.1 应用程序和 UWP 应用程序之间的 SHA1 哈希值不同

标签 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 应用程序,我正在做:

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();

我没有得到相同的结果。

例如,如果我有文本“Windows 8”,我会得到

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)。

关于c# - Windows Phone 8.1 应用程序和 UWP 应用程序之间的 SHA1 哈希值不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38680785/

相关文章:

c# - 如何让 OmniSharp VSCode intellisense 与 WSL 编译的代码一起工作?

c# - Unity - 如何快进到动画的百分比?

javascript - C# Linq SelectMany,TypeScript 等价物

xaml - 更改 UWP 评级控件的大小和间距

MySQL哈希列密码+脚本bash

c# - Asp .Net Core 为什么即使用户未通过身份验证也会调用我的授权处理程序?

get - UWP:从 'get; & set;' 中的 FilePath 设置 image.source

c# - UWP 不支持的 API

php - sha1(密码)加密

在 Windows 操作系统中计算大文件的 SHA1 哈希值时 Python 崩溃