smsware
smsware

Reputation: 479

Hashing text with SHA-256 at Windows Forms

String inputPass = textBox2.Text;
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(inputPass);
byte[] inputHashedBytes = Sha256.ComputeHash(inputBytes);
String inputHash = Convert.ToBase64String(inputHashedBytes);

I'm getting some strange output:

Q9nXCEhAn7RkIOVgBbBeOd5LiH7FWFtDFJ22TMLSoH8=

By output hash looks like this:

43d9d70828409fb46420e56005b05e38de4b887ec5585b43149db64cc2d2a07f

Upvotes: 2

Views: 4383

Answers (2)

Michael
Michael

Reputation: 3831

// This is where you get the actual binary hash
byte[] inputHashedBytes = Sha256.ComputeHash(inputBytes);

// But you want it in a string format, similar to a variety of Unix tools
string result = BitConverter.ToString(inputHashedBytes)
   // This will remove all the dashes in between each two characters
   .Replace("-", string.Empty)
   // And make it lowercase
   .ToLower();

Upvotes: 7

SLaks
SLaks

Reputation: 888177

Encoding.UTF8.GetString parses bytes as UTF-8 code points.

The SHA-256 hash is an arbitrary 256-bit number and does not correspond to any Unicode text.

You probably want to show the binary value in hexadecimal, by calling BitConverter.ToString(). You can also call Convert.ToBase64String().

Upvotes: 7

Related Questions