0

Below is the code I use for hashing and I am trying to store the hash in a string(strSHA256) with UTF-8 encoding.

using (SHA256 sha256Hash = SHA256.Create())
{
   bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(utf8EncodedString));
   strSHA256 = Encoding.UTF8.GetString(bytes);               
}

When I inspect the string variable I see "�u��Ou\u0004����O��zA�\u0002��\0�\a�}\u0012�L�Dr�" type of value. My question is regarding to ? character marked in bold. They should be UTF-8 chars. Why cant I view the UTF-8 special chars when I inspect from visual studio. The string o/p contains this weird char as well.

Note: I tried SHA256 with Node Js and I tend to see perfect UTF-8 special chars.

bhanu.cs
  • 1,345
  • 1
  • 11
  • 25
  • 3
    What do you expect strSHA256 to look like? You're trying to display an array of essentially random bytes as UTF-8, that's not going to result in printable output. – Jonas Høgh Feb 01 '20 at 08:14
  • 2
    **Bytes is not UTF-8, so treating it as such is invalid**. It’s binary junk that is being treated as UTF-8. The “?” is a placeholder when there is not a suitable glyph for rendering the character, which depends on what is doing the rendering and in what font, etc. – user2864740 Feb 01 '20 at 08:20
  • Oh yeah!Got you. Thanks. What am I thinking. – bhanu.cs Feb 01 '20 at 08:25

1 Answers1

0

As @user2864740 mentioned bytes are not UTF8. Use base64 for bytes string representation.

       var hello = "Hello world!";
        using (var sha256Hash = SHA256.Create())
        {
            var bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(hello));

            // String representation
            var stringForBytes  = Convert.ToBase64String(bytes);

            // HEX string representation
            var str = new StringBuilder(bytes.Length * 2);
            foreach (var b in bytes)
            {
                str.Append(b.ToString("X2"));
            }

            var hexString = str.ToString();
        }
Denis Kucherov
  • 369
  • 3
  • 15