Function Membuat MD5 di C#
Kode C# dibawah ini berguna untuk membuat MD5 hash —string 32-character angka hexadecimal— dari sebuah string. Biasanya digunakan untuk menyimpan password di databases sebagai hash code. MD5 hash—yang didefinisikan pada RFC 1321:MD5 Message-Digest Algorithm—merupakan algoritma hash yang umum yang dapat digunakan dengan mudah di oleh programmer C# .
Untuk menggunakannya, cukup dipanggil sebagai berikut:
string md5hash = CreateMD5Hash ("abcdefghijklmnopqrstuvwxyz");
md5hash akan berisi string “C3FCD3D76192E4007DFB496CCA67E13B“.
using System; using System.Security.Cryptography; using System.Text; public string CreateMD5Hash (string input) { // Use input string to calculate MD5 hash MD5 md5 = System.Security.Cryptography.MD5.Create(); byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes (input); byte[] hashBytes = md5.ComputeHash (inputBytes); // Convert the byte array to hexadecimal string StringBuilder sb = new StringBuilder(); for (int i = 0; i < hashBytes.Length; i++) { sb.Append (hashBytes[i].ToString ("X2")); // To force the hex string to lower-case letters instead of // upper-case, use he following line instead: // sb.Append(hashBytes[i].ToString("x2")); } return sb.ToString(); }
Akhmad Daniel Sembiring
vITraining.com – Qualified IT Products, Outsourcing, and Services
Ligarwangi.com – Linux, E-book, Coffee, Gift, etc



Recent Comments