PCLCrypto MD5 HashAlgorithm

Значение, возвращаемое HashData не является примером хэша Md5:

Хеширование "всегда возвращайся"0cc175b9c0f1b6a831c399e269772661"

Но этот код всегда возвращает другое значение.

private byte[] GetHash(string data)    
{
      IHashAlgorithmProvider algoProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Md5);
      byte [] dataTB = Encoding.UTF8.GetBytes(data);
      return algoProv.HashData(dataTB);
}

1 ответ

Решение

Я проверил MD5 алгоритм из PCLCrypto и это сработало, как и ожидалось. Всегда печатается0cc175b9c0f1b6a831c399e269772661"

for (int i = 0; i< 10; i++)
{
    Debug.WriteLine(ByteArrayToHex((GetHash("a"))));
}

public static byte[] GetHash(string data)
{
    IHashAlgorithmProvider algoProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Md5);
    byte[] dataTB = Encoding.UTF8.GetBytes(data);
    return algoProv.HashData(dataTB);
}

//Convert hash to hex
private static string ByteArrayToHex(byte[] hash)
{
    var hex = new StringBuilder(hash.Length * 2);
    foreach (byte b in hash)
        hex.AppendFormat("{0:x2}", b);

    return hex.ToString();
}
Другие вопросы по тегам