Question

If I want the following result :

RIPEMD-160("The quick brown fox jumps over the lazy dog") =
 37f332f68db77bd9d7edd4969571ad671cf9dd3b

I tried this :

string hash11 = System.Text.Encoding.ASCII.GetString(RIPEMD.ComputeHash(Encoding.ASCII.GetBytes("The quick brown fox jumps over the lazy dog")));

but it doesn't give me the previous result!

Was it helpful?

Solution

The ComputeHash function gives you a byte array with the values in it (0x37, 0xF3, ...). If you use GetString it will take every value in the byte and use the character with that value, it will not convert the value into a string.

You could convert it like that:

var bytes = RIPEMD.ComputeHash(Encoding.ASCII.GetBytes("The quick brown fox jumps over the lazy dog"));
string hash11 = "";
foreach(var curByte in bytes)
    hash11 = curByte.ToString("X2") + hash11; // or curByte.ToString("X") if for example 9 should not get 09

Like that you have the highest byte at the beginning. With

hash11 += curByte.ToString("X2")

you have the lowest byte at the beginning.

OTHER TIPS

What you want to get is the hex representation of the byte array: each byte should be represented by its two-character hex value.

You can check this thread for several different examples on how to do that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top