gpt4 book ai didi

c# - 如何在 UWP 中散列文件(MD5、SHA..v.v..)

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:16 25 4
gpt4 key购买 nike

我正在编写通用应用程序,如何使用 md5SHA 算法对文件进行哈希处理?

我搜索了一下,发现了这个:system.security.cryptography,但它在我的项目中不可用。

我使用的是 Visual Studio 2015。

最佳答案

在 UWP 中,它是 Windows.Security.Cryptography namespaceWindows.Security.Cryptography.Core namespace .

CryptographicBuffer class有示例显示如何使用此类。

这是我关于获取 MD5 哈希的演示:

private string strAlgNameUsed;

public string GetMD5Hash(String strMsg)
{
string strAlgName = HashAlgorithmNames.Md5;
IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strMsg, BinaryStringEncoding.Utf8);

HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(strAlgName);
strAlgNameUsed = objAlgProv.AlgorithmName;

IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);

if (buffHash.Length != objAlgProv.HashLength)
{
throw new Exception("There was an error creating the hash");
}

string hex = CryptographicBuffer.EncodeToHexString(buffHash);

return hex;
}

关于c# - 如何在 UWP 中散列文件(MD5、SHA..v.v..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40523475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com