MD5 and SHA-1 (to Base-64)
MD5 and SHA-1 produces a hash signature, and the output is typically show in a hex format or a Base-64. In this example the output is converted into a Base-64 format. The following is an example:
Message:
As a test, MD5 should give:
"Hello": ixqZU8RhEpaoJ6v4xHgE1w==
and "hello": XUFAKrxLKna5cZ2REBfFkg==
With SHA-1:
"Hello" should give: 9/+ei3uy4Jtwk1pdeF4MxdnQq/A=
"hello" should give: qvTGHdzF6KLavt4PO0gs2a6pQ00=
The code uses the Convert.ToBase64String() method to convert from a byte array to a Hash-64 string:
string message;
message = this.tbMessage.Text;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
MD5 md5 = new MD5CryptoServiceProvider();
SHA1 sha1 = new SHA1CryptoServiceProvider();
SHA256Managed sha256 = new SHA256Managed();
SHA384Managed sha384 = new SHA384Managed();
SHA512Managed sha512 = new SHA512Managed();
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = md5.ComputeHash(messageBytes);
this.tbMD5.Text = Convert.ToBase64String(hashmessage);
hashmessage = sha1.ComputeHash(messageBytes);
this.tbSHA1.Text = Convert.ToBase64String(hashmessage);
hashmessage = sha256.ComputeHash(messageBytes);
this.tbSHA256.Text = Convert.ToBase64String(hashmessage);
hashmessage = sha384.ComputeHash(messageBytes);
this.tbSHA384.Text = Convert.ToBase64String(hashmessage);
hashmessage = sha512.ComputeHash(messageBytes);
this.tbSHA512.Text = Convert.ToBase64String(hashmessage);
|