c# - WindowsPhone 8 中的 MD5 散列

标签 c# hash windows-phone md5

嘿,我正试图在 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/

相关文章:

java - 任何快速简单的方法来散列小字节数组,以便最重要的位是最可变的?

python - 在Python中使用唯一键对字符串进行哈希处理的安全性

android - Xamarin.Forms 是否支持点击并按住手势?

c# - WorklistQuery (C-FIND) 返回 C-Find 响应 [2] : Optional Keys Not Supported

c# - String 和 StringBuilder 的性能注意事项 - C#

security - 安全 token URL - 它有多安全?代理身份验证作为替代方案?

windows-phone - IpOverUsbSvc 错误

c# - 是否可以在 Windows Phone 7/8 上使用 protobuf-net 序列化/反序列化不可变类型?

c# - 如何使用 MemoryCache 代替 Timer 来触发方法?

c# - protobuf 网络 : how to annotate properties of derived type?