【问题标题】:SHA1 Hash not the same between Windows Phone 8.1 app and UWP appWindows Phone 8.1 应用和 UWP 应用之间的 SHA1 哈希值不同
【发布时间】:2016-07-31 12:45:16
【问题描述】:

在 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”,我会得到 ​​p>

6517856f8c3a3fda3ae28305a05d127f0e1bdb97 (Windows Phone 8.1)

zrefb4w6p9o64omfof0sfw4b25c= (UWP)

我不太清楚自己做错了什么。目标是获取字符串的 SHA1 哈希值。

【问题讨论】:

  • EncodeToBase64String换成EncodeToHexString,结果是一样的。

标签: c# uwp sha1 windows-10-universal


【解决方案1】:

您可能希望使用以下命令编码为十六进制,而不是在 UWP 代码 (CryptographicBuffer.EncodeToBase64String(buffHash)) 下编码为 base64:

string strHex = CryptographicBuffer.EncodeToHexString(buffHash);
string hash = strHex.ToLower();

因此,您的 Windows Phone 8 代码和 UWP 代码都将产生相同的输出(十六进制编码 SHA1)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2023-02-19
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多