MD5 and SHA-1
MD5 and SHA-1 methods produces a hash signature, and are the two of the most
widely used methods. The MD5 algorithm has been show to have weaknesses, and a
collision of message hashes has been shown to occur in less than one day. An MD5
signature has 128 bits, an SHA-1 signature has 160 bits, and an SHA-256
signature has 256 bits. [Lecture][Tutorial] The following is an example:
Message:
MD5:
SHA-1:
SHA-256:
SHA-384:
SHA-512:
As a test: "Hello" should give: 8b1a9953c4611296a827abf8c47804d7
and "hello" should give: 5d41402abc4b2a76b9719d911017c592
The code is:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Security.Cryptography;
public partial class _Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
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 = ByteToString(hashmessage);
hashmessage = sha1.ComputeHash(messageBytes);
this.tbSHA1.Text = ByteToString(hashmessage);
hashmessage = sha256.ComputeHash(messageBytes);
this.tbSHA256.Text = ByteToString(hashmessage);
hashmessage = sha384.ComputeHash(messageBytes);
this.tbSHA384.Text = ByteToString(hashmessage);
hashmessage = sha512.ComputeHash(messageBytes);
this.tbSHA512.Text = ByteToString(hashmessage);
}
public static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2"); // hex format
}
return (sbinary);
}
}
|