我为UWP编写应用程序
我有密码
private string Hash(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
// can be "x2" if you want lowercase
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
}但是它不起作用,并且显示了这个错误。
严重程度代码描述项目文件行抑制状态错误无法找到类型或命名空间名称'SHA1Managed‘(您是缺少一个使用指令还是程序集引用?)米兰C:\Users\nemes\Documents\GitHub\Milano_pizza\Milano\WoocommerceApiClient.cs 25活动
我怎么才能解决这个问题?
发布于 2016-08-11 16:04:15
对于UWP,使用HashAlgorithmProvider
public string Hash(string input)
{
IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8);
HashAlgorithmProvider hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
var hashByte = hashAlgorithm.HashData(buffer).ToArray();
var sb = new StringBuilder(hashByte.Length * 2);
foreach (byte b in hashByte)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}记住要加上
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;发布于 2018-04-11 09:29:17
SHA1Managed只适用于安卓和iOs,对于Windows的使用:
如果因此需要一个字节数组:
public byte[] getSHA1MessageDigest(string originalKey)
{
IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(originalKey, BinaryStringEncoding.Utf8);
HashAlgorithmProvider hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
IBuffer sha1 = hashAlgorithm.HashData(buffer);
byte[] newByteArray;
CryptographicBuffer.CopyToByteArray(sha1, out newByteArray);
return newByteArray;
}如果因此需要一个字符串:
public string getSHA1MessageDigest(string originalKey)
{
IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(originalKey, BinaryStringEncoding.Utf8);
HashAlgorithmProvider hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
IBuffer sha1 = hashAlgorithm.HashData(buffer);
byte[] newByteArray;
CryptographicBuffer.CopyToByteArray(sha1, out newByteArray);
var sb = new StringBuilder(newByteArray.Length * 2);
foreach (byte b in newByteArray)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}https://stackoverflow.com/questions/38897468
复制相似问题