Tha*_*150 1 .net c# hash visual-studio-2015 windows-10-universal
我正在编写通用应用程序,如何使用md5或SHA算法散列文件?
我搜索了一下,发现了这个:system.security.cryptography但是我的项目中没有它.
我正在使用Visual Studio 2015.
在UWP中,它是Windows.Security.Cryptography命名空间和Windows.Security.Cryptography.Core命名空间.
在CryptographicBuffer类中,有一个示例显示如何使用此类.
这是关于获取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;
}
Run Code Online (Sandbox Code Playgroud)