嘿,我正试图在 Windows Phone 中将字符串散列为 MD5 ...但是当我调用 MD5 类时,出现以下错误
The type or namespace name 'MD5' could not be found (are you missing a using directive or an assembly reference?)
PS:我使用了System.Security.Cryptography命名空间
那么我如何在 Windows Phone 中使用 MD5 哈希值呢?
这是我的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace FluoraPin
{
class HASHING
{
public static string GetMd5Hash(MD5 md5Hash, string input)
{
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
// t verify md5 hashing
private bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
{
// Hash the input.
string hashOfInput = GetMd5Hash(md5Hash, input);
// Create a StringComparer an compare the hashes.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}
}
}
最佳答案
我认为错误的答案是正确的:
The type or namespace name 'MD5' could not be found (are you missing a using directive or an assembly reference?)
MD5
不是 Windows Phone 的 System.Security.Cryptography
命名空间中的类。参见 MSDN's System.Security.Cryptography
page for Windows Phone进行确认。
将此与 MSDN's general System.Security.Cryptography
page, 对比它将 MD5
列为命名空间中的一个类。
话虽如此,你真的应该使用SHA-256或更高版本,而不是 MD5 或 SHA-1 哈希。
SHA-256 哈希可通过 SHA256Managed
类用于 Windows Phone 7 和 8 - 在您已经使用的 Security.Security.Cryptography
命名空间中。有关如何使用 SHA256Managed
的示例,请参阅 answer to a related SO question .
关于c# - WindowsPhone 8 中的 MD5 散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21963272/