SHA256 hash string in C # disagrees with hash on website

If I use the string "password" in C # using SHA256 using the method below, I get this as my output:

e201065d0554652615c320c00a1d5bc8edca469d72c2790e24152d0c1e2b6189 

But this website (SHA-256 creates a 256-bit (32-byte) hash value) tells me:

 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 

I obviously have a problem with the data format or something similar. Any ideas why this C # SHA256Managed method returns something else? I am sending the password method as an input parameter.

  private static string CalculateSHA256Hash(string text) { UnicodeEncoding UE = new UnicodeEncoding(); byte[] hashValue; byte[] message = UE.GetBytes(text); SHA256Managed hashString = new SHA256Managed(); string hex = ""; hashValue = hashString.ComputeHash(message); foreach (byte x in hashValue) { hex += String.Format("{0:x2}", x); } return hex; } 
+6
source share
1 answer

UnicodeEncoding is UTF-16, which guarantees that all your characters are inflated to two to four bytes to represent their Unicode code point (see also the MSDN Notes section).

Use (static) Encoding.UTF8 , so most characters will fit in one byte. It depends on which system you need to emulate, whether it will work for all lines.

+13
source

All Articles