PCLCrypto MD5 HashAlgorithm

1.8k Views Asked by At

The value returned by HashData is not a Md5 hash example:

Hashing the "a" always return "0cc175b9c0f1b6a831c399e269772661"

But this code always returns a different value.

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

There are 1 best solutions below

3
jzeferino On BEST ANSWER

I've tested the MD5 algorithm from PCLCrypto and it worked as expected. Always printed "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();
}