/* * Penerapan HMAC MD5 pada Microsoft .NET Framework 1.1 * Versi 1.0 Hakcipta (C) Ahmad Masykur 2005 * http://cahnom.blogspot.com/ * Anda diperbolehkan menggunakan/merubah kode program ini * secara bebas namun harus tetap menyebutkan nama pembuat asli * pada kepala program ini */ using System; using System.Text; using System.Security.Cryptography; namespace cahnom { /// /// Summary description for Cryptography. /// public class HMACMD5 { public HMACMD5() { // // TODO: Add constructor logic here // } // Fungsi untuk menggabungkan dua larik byte private byte[] ConcateBytes(byte[] a, byte[] b) { byte[] result = new byte[a.Length + b.Length]; a.CopyTo(result, 0); b.CopyTo(result, a.Length); return result; } public byte[] ComputeHash(string Key, string Data) { MD5CryptoServiceProvider md5prov = new MD5CryptoServiceProvider(); System.Text.ASCIIEncoding enc = new ASCIIEncoding(); byte[] bkey = enc.GetBytes(Key); byte[] bdata = enc.GetBytes(Data); // jika panjang kunci > panjang blok if (bkey.Length > 64) { // hitung hash bkey = md5prov.ComputeHash(bkey); // tambahkan 0 di akhir kunci sehingga panjang kunci=panjang blok byte[] bpad = new byte[64 - bkey.Length]; for (int i=0; i < bpad.Length; i++) bpad[i] = 0; bkey = ConcateBytes(bkey, bpad); } // jika panjang kunci < panjang blok if (bkey.Length < 64) { // tambahkan 0 di akhir kunci sehingga panjang kunci=panjang blok byte[] bpad = new byte[64 - bkey.Length]; for (int i=0; i < bpad.Length; i++) bpad[i] = 0; bkey = ConcateBytes(bkey, bpad); } byte[] ipad = new byte[64]; byte[] opad = new byte[64]; for (int i=0; i < 64; i++) ipad[i] = 0x36; for (int i=0; i < 64; i++) opad[i] = 0x5C; for(int i = 0; i < 64; i++) { ipad[i] ^= Convert.ToByte(bkey[i]); opad[i] ^= Convert.ToByte(bkey[i]); } byte[] hash = md5prov.ComputeHash(ConcateBytes(ipad, bdata)); return md5prov.ComputeHash(ConcateBytes(opad, hash)); } } }