![]() |
For more information on the MD5 algorithm and pseudocode, visit the Wikipedia article.
Snippets[]
.NET Framework[]
(built-in) The System.Security.Cryptography.MD5CryptoServiceProvider class can be used, especially overloads of ComputeHash. Here is an example in C# that takes in a filename and outputs the hash:
using System.Security.Cryptography; ... string MD5hash(string filename) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider; byte[] hash; using (FileStream s = File.OpenRead(filename)) { hash = md5.ComputeHash(s); } string r = BitConverter.ToString(hash); return r; }
APL[]
hash ← MD5.Hash msg
APL has no primitive function for MD5 hashes. For industrial use, an implementation closer to assembler (eg C) would perform faster.
But APL was originally devised for describing algorithms. Here is an executable (and tested) description in Dyalog APL. There is a longer article on the APL Wiki.
:Class MD5 ⍝ create a message digest with the MD5-algorithm (RFC1321) ⍝ static class: no instances ⍝ see also http://en.wikipedia.org/wiki/Md5 ⍝ Written 07.07.1999 Conrad Hoesle-Kienzlen <chk@hoesle-kienzlen.de> ⍝ Revised 17.03.2002 Brian W. Oliver <bwo@aplborealis.com> ⍝ Revised 09.04.2008 Stephen Taylor <sjt@dyalog.com> ⍝----------------------------------- public methods ∇ r←Hash msg;chunk;bits;rawbits;ABCD;chunks;start ⍝ msg: message of arbitrary length ⍝ r: digest, always 16 hex digits (32 characters) :Access Public Shared rawbits←,⍉(8/2)⊤¯1+ASCII⍳msg ⍝ convert message to binary bits←512{⍵↑⍨⍺×⊃0 ⍺⊤⊃⍴⍵}rawbits,512↑1 ⍝ pad to multiple of 512 bits (¯64↑bits)←,⊖8 8⍴,(64⍴2)⊤⍴rawbits ⍝ write length at end ABCD←INITIALSTATES ⍝ convert to decimal word length, chunks←16 cols 2⊥⍉(32 cols bits)[;,24 16 8 0∘.+⍳8] ⍝ reverse byte-order, encode to decimal :For chunk :In ↓chunks start←ABCD ⍝ initial state for this chunk ABCD←ABCD(chunk round F)Fshifts ⍝ round F ABCD←ABCD(chunk round G)Gshifts ⍝ round G ABCD←ABCD(chunk round H)Hshifts ⍝ round H ABCD←ABCD(chunk round I)Ishifts ⍝ round I ABCD{MAX|⍺+⍵}←start ⍝ add to initial cycle state :EndFor r←⊃,/hex¨ABCD ∇ ∇ ok←SelfTest :Access Public Shared :If ok←'0cc175b9c0f1b6a831c399e269772661'≡Hash'a' :AndIf ok←'d41d8cd98f00b204e9800998ecf8427e'≡Hash'' :AndIf ok←'7215ee9c7d9dc229d2921a40e899ec5f'≡Hash' ' :AndIf ok'f96b697d7cb7938d525a2f31aaf161d0'≡Hash'message digest' ok←'9e107d9d372bb6826bd81d3542a419d6'≡Hash'The quick brown fox jumps over the lazy dog' :EndIf ∇ ⍝----------------------------------- vocabulary ⎕IO ⎕ML←1 0 MAX←4294967296 ⍝ maximum integer ASCII←⎕AV[⍋1+⎕NXLATE 0] ⍝ ASCII character string cols←{⍵⍴⍨((⍴⍵)÷⍺),⍺} ⍝ reshape in ⍺ cols bin←(32/2)∘⊤ ⍝ convert to 32-bit binary hex←{'0123456789abcdef'[1+,⌽4 2⍴⌽(8/16)⊤⍵]} ⍝ convert to hex CONVERSIONTABLE←⌊MAX×|1○⍳64 INITIALSTATES←1732584193 4023233417 2562383102 271733878 ⍝ initial variable states ⍝ '67452301'h 'efcdab89'h '98badcfe'h '10325476'h (low byte order) F←{X Y Z←⍵ ⋄ (X∧Y)∨(~X)∧Z} ⍝ encoding function G←{X Y Z←⍵ ⋄ (X∧Z)∨Y∧~Z} ⍝ encoding function H←{X Y Z←⍵ ⋄ X≠Y≠Z} ⍝ encoding function I←{X Y Z←⍵ ⋄ Y≠X∨~Z} ⍝ encoding function ⍝ cf http://en.wikipedia.org/wiki/Md5#Algorithm apply←{ ⍝ apply encoding function ⍺⍺ A B C D k s i←⍵ ⍝ with arguments ⍵ B+2⊥s⌽bin ⍺[k]+CONVERSIONTABLE[i]+A+2⊥⍺⍺ bin¨B C D ⍝ to message chunk ⍺ } ∇ ABCD←ABCD(chunk round fn)shifts;tgt;rot;shft ⍝ perform a round on chunk :For tgt rot shft :InEach (16⍴1 4 3 2)(1-⍳16)(shifts) ⍝ using fn and shifts, ABCD[tgt]←chunk(fn apply)(rot⌽ABCD),shft ⍝ modifying ABCD :EndFor ∇ Fshifts←(1 7 1)(2 12 2)(3 17 3)(4 22 4) ⍝ shifts for round F Fshifts,←(5 7 5)(6 12 6)(7 17 7)(8 22 8) Fshifts,←(9 7 9)(10 12 10)(11 17 11)(12 22 12) Fshifts,←(13 7 13)(14 12 14)(15 17 15)(16 22 16) Gshifts←(2 5 17)(7 9 18)(12 14 19)(1 20 20) ⍝ shifts for round G Gshifts,←(6 5 21)(11 9 22)(16 14 23)(5 20 24) Gshifts,←(10 5 25)(15 9 26)(4 14 27)(9 20 28) Gshifts,←(14 5 29)(3 9 30)(8 14 31)(13 20 32) Hshifts←(6 4 33)(9 11 34)(12 16 35)(15 23 36) ⍝ shifts for round H Hshifts,←(2 4 37)(5 11 38)(8 16 39)(11 23 40) Hshifts,←(14 4 41)(1 11 42)(4 16 43)(7 23 44) Hshifts,←(10 4 45)(13 11 46)(16 16 47)(3 23 48) Ishifts←(1 6 49)(8 10 50)(15 15 51)(6 21 52) ⍝ shifts for round I Ishifts,←(13 6 53)(4 10 54)(11 15 55)(2 21 56) Ishifts,←(9 6 57)(16 10 58)(7 15 59)(14 21 60) Ishifts,←(5 6 61)(12 10 62)(3 15 63)(10 21 64) :EndClass
C[]
MD5 code in C by Ulrich Drepper
public static string MD5_ComputeHexaHash (string text) { // Gets the MD5 hash for text MD5 md5 = new MD5CryptoServiceProvider(); byte[] data = Encoding.Default.GetBytes(text); byte[] hash = md5.ComputeHash(data); // Transforms as hexa string hexaHash = ""; foreach (byte b in hash) { hexaHash += String.Format("{0:x2}", b); } // Returns MD5 hexa hash return hexaHash; }
Java[]
(built-in)
import java.security.MessageDigest; public static String md5sum(byte[] convertme) { MessageDigest md = MessageDigest.getInstance("MD5"); return new String(md.digest(convertme)); }
OCaml[]
(built-in) The following is an example of an MD5 checksum in OCaml.
let converted = Digest.to_hex (Digest.string "My text");;
Perl[]
(built-in) The following is an example of an MD5 checksum in Perl.
# Functional style use Digest::MD5 qw(md5 md5_hex md5_base64); $digest = md5($data); $digest = md5_hex($data); $digest = md5_base64($data);
# OO style use Digest::MD5; $ctx = Digest::MD5->new; $ctx->add($data); $ctx->addfile(*FILE); $digest = $ctx->digest; $digest = $ctx->hexdigest; $digest = $ctx->b64digest;
PHP[]
(built-in) The following is an example of an MD5 checksum in PHP.
$digest = md5($data);
$digest = hash('md5',$data);
Speed comparison against md5() function Note that hash() is available in PHP 5 and above.
Python[]
(built-in) The following is an example of an MD5 checksum in Python.
import hashlib converted = hashlib.md5("My text").hexdigest()
Ruby[]
require 'digest/md5' puts Digest::MD5.hexdigest("Hello World")
Tcl[]
(built-in) The following is an example of an MD5 checksum in Tcl.
package require md5 set digest [md5::md5 -hex "My Text"]
# For performing incremental digest package require md5 set tok [md5::MD5Init] md5::MD5Update $tok "Tcl " md5::MD5Update $tok "does " md5::MD5Update $tok "MD5" set digest [md5::MD5Final $tok]
See also[]
External links[]
MD5 at Wikipedia
Java MD5 Example
Browser based file hasher
Online MD5 Hash Generator