【发布时间】:2014-02-23 02:09:11
【问题描述】:
嘿,我正在尝试在 windows phone 中将字符串散列到 MD5 ......但是当我调用 MD5 类时,我收到以下错误
找不到类型或命名空间名称“MD5”(您是否缺少 使用指令还是程序集引用?)
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;
}
}
}
}
【问题讨论】:
-
不是答案,而是:我假设您知道 MD5 已损坏?
-
nooop 我不知道!谢谢你有什么建议?
-
使用SHA-256,通过
SHA526Managedclass 可用于Windows Phone 7 和8。 answer to another SO question 提供了如何在 C# 中使用SHA256Managed进行散列的示例。
标签: c# hash windows-phone md5