2014-02-23 107 views
2

干草有我想要散列Windows Phone的一个字符串的MD5 ......但是当我调用MD5类我得到以下错误MD5哈希中的WindowsPhone 8

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; 
      } 
     } 
    } 
} 
+2

没有答案,但:我假设你知道MD5坏了? –

+0

nooop我不知道!谢谢你建议什么? – a3adel

+0

使用[SHA-256](http://en.wikipedia.org/wiki/SHA-256),可通过['SHA526Managed'类](http://msdn.microsoft .COM/EN-US /库/ WindowsPhone的/开发/ system.security.cryptography.sha256managed(v = vs.105)的.aspx)。 [另一个SO问题的答案](http://stackoverflow.com/a/1756222/1810429)提供了一个如何在C#中用SHA256Managed进行散列的例子。 – J0e3gan

回答

-4

我没有测试您的解决方案,但我发现,我工作得很好的解决方案。

using System.Security.Cryptography;

class MD5Hash 
{ 
    public String getHash(String input) 
    { 
     MD5 md5 = System.Security.Cryptography.MD5.Create(); 
     byte[] inputBytes = Encoding.ASCII.GetBytes(input); 
     byte[] hash = md5.ComputeHash(inputBytes); 

     StringBuilder sb = new StringBuilder(); 

     for (int i = 0; i < hash.Length; i++) 
      sb.Append(hash[i].ToString("x2")); 

     return sb.ToString(); 
    } 

    public Boolean VerifyHash(String input, String hash) 
    { 
     String hashOfInput = getHash(input); 

     StringComparer comparer = StringComparer.OrdinalIgnoreCase; 

     if (0 == comparer.Compare(hashOfInput, hash)) 
      return true; 
     else 
      return false; 
    } 
} 

这会出现乱码的字符串,完全没有错误。

此外,您收到错误,请检查您是否正在编译包含文本“客户端配置文件”的.Net版本。

我是新来的,所以如果我得到了这个完全错误的,那么我很抱歉你能对你提出更具体的问题。

+0

System.Security.Cryptography.MD5在windows phone上不可用 –

2

我想答案是正确的错误:

The type or namespace name 'MD5' could not be found (are you missing a using directive or an assembly reference?)

MD5不在System.Security.Cryptography命名为Windows Phone的一类。请参阅MSDN's System.Security.Cryptography page for Windows Phone进行确认。

将此与MSDN's general System.Security.Cryptography page,对比,其中将MD5列为命名空间中的类。

话虽如此,你应该真的使用SHA-256或更高版本而不是MD5或SHA-1哈希。

SHA-256 hashing可用于Windows Phone 7和8到SHA256Managed类 - 在您已使用的Security.Security.Cryptography名称空间中。有关如何使用SHA256Managed的示例,请参阅answer to a related SO question

+1

但我需要MD5来签名API查询 –

+0

这是一个非常明显的答案。如何试图解决这个问题? –