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. The following is an example:
As a test: "Hello" should give: 8b1a9953c4611296a827abf8c47804d7
and "hello" should give: 5d41402abc4b2a76b9719d911017c592
With SHA-1:
As a test: "Hello" should give: f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0
and "hello" should give: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
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);
}
}
|