MD5: Difference between revisions

40,033 bytes added ,  3 months ago
Added Easylang
(Added Easylang)
 
(15 intermediate revisions by 12 users not shown)
Line 15:
 
=={{header|8th}}==
<langsyntaxhighlight lang="forth">
"md5" cr:hash! "Some text" cr:hash cr:hash>s
. cr bye
</syntaxhighlight>
</lang>
{{out}}
<tt>
Line 24:
</tt>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program MD5_64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ MD5_DIGEST_LENGTH, 16
.equ ZWORKSIZE, 1000
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessRosetta: .asciz "Rosetta Code"
szMessTest1: .asciz ""
szMessTest2: .asciz "abc"
szMessTest3: .asciz "abcdefghijklmnopqrstuvwxyz"
szMessTest4: .asciz "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
szMessTest5: .asciz "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
szMessFinPgm: .asciz "Program End ok.\n"
szMessResult: .asciz "Result for "
szMessResult1: .asciz " => "
szMessSizeError: .asciz "\033[31mWork area too small !! \033[0m \n"
szCarriageReturn: .asciz "\n"
 
/* array constantes K */
tbConstK: .int 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee
.int 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501
.int 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be
.int 0x6b901122,0xfd987193,0xa679438e,0x49b40821
.int 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa
.int 0xd62f105d,0x2441453,0xd8a1e681,0xe7d3fbc8
.int 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed
.int 0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a
.int 0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c
.int 0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70
.int 0x289b7ec6,0xeaa127fa,0xd4ef3085,0x4881d05
.int 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665
.int 0xf4292244,0x432aff97,0xab9423a7,0xfc93a039
.int 0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1
.int 0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1
.int 0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391
 
/* array rotation coef R */
tbRotaR: .int 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22
.int 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20
.int 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23
.int 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
 
tbConstH: .int 0x67452301 // H0
.int 0xEFCDAB89 // H1
.int 0x98BADCFE // H2
.int 0x10325476 // H3
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
//iNbBlocs: .skip 8
sZoneConv: .skip 24
sZoneResult: .skip 24
tbH: .skip 4 * 4 // 4 variables H
sZoneTrav: .skip ZWORKSIZE
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessTest1
bl computeExemple
ldr x0,qAdrszMessTest2
bl computeExemple
ldr x0,qAdrszMessTest3
bl computeExemple
 
ldr x0,qAdrszMessTest4
bl computeExemple
ldr x0,qAdrszMessTest5
bl computeExemple
ldr x0,qAdrszMessFinPgm
bl affichageMess // display message
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessResult: .quad szMessResult
qAdrszMessResult1: .quad szMessResult1
qAdrszMessRosetta: .quad szMessRosetta
qAdrszMessTest1: .quad szMessTest1
qAdrszMessTest2: .quad szMessTest2
qAdrszMessTest3: .quad szMessTest3
qAdrszMessTest4: .quad szMessTest4
qAdrszMessTest5: .quad szMessTest5
qAdrsZoneTrav: .quad sZoneTrav
qAdrsZoneConv: .quad sZoneConv
qAdrszMessFinPgm: .quad szMessFinPgm
/***********************************************/
/* compute exemple */
/***********************************************/
/* x0 contains the address of the message */
computeExemple:
stp x18,lr,[sp,-16]! // save registers
mov x18,x0
bl computeMD5 // call routine MD5
ldr x0,qAdrszMessResult
bl affichageMess
mov x0,x18
bl affichageMess
ldr x0,qAdrszMessResult1
bl affichageMess
ldr x0, qAdrsZoneResult
bl displayMD5
100:
ldp x18,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* compute MD5 */
/******************************************************************/
/* x0 contains the address of the message */
computeMD5:
stp x1,lr,[sp,-16]! // save registers
ldr x1,qAdrsZoneTrav
mov x2,#0 // counter length
1: // copy string in work area
cmp x2,ZWORKSIZE
bge 99f
ldrb w3,[x0,x2]
strb w3,[x1,x2]
cmp x3,#0
add x4,x2,1
csel x2,x4,x2,ne
bne 1b
// add bit et compute length
lsl x6,x2,#3 // initial message length in bits
mov x3,#0b10000000 // add bit 1 at end of string
strb w3,[x1,x2]
add x2,x2,#1 // length in bytes
lsl x4,x2,#3 // length in bits
mov x3,#0
2:
lsr x5,x2,#6 // padding block 512 bytes
lsl x5,x5,#6
sub x5,x2,x5
cmp x5,#56
beq 3f // yes -> end add
strb w3,[x1,x2] // add zero at message end
add x2,x2,#1 // increment length bytes
add x4,x4,#8 // increment length in bits
b 2b
3:
str x6,[x1,x2] // and store at end
ldr x7,qAdrtbConstH // constantes H address
ldr x4,qAdrtbH // start area H
mov x5,#0
4: // init array H with start constantes
ldr w6,[x7,x5,lsl #2] // load constante
str w6,[x4,x5,lsl #2] // and store
add x5,x5,#1
cmp x5,#4 // constantes number
blt 4b
// split into block of 64 bytes
add x2,x2,#4 // TODO : à revoir
lsr x4,x2,#6 // blocks number
mov x7,#0 // no de block et x1 contient l'adresse zone de travail
ldr x3,qAdrtbConstK // K constantes address
ldr x5,qAdrtbRotaR // R rotation address
5: // begin loop of each block of 64 bytes
// init variable a b c d with H0 H1 H2 H3
ldr x0,qAdrtbH
ldr w8,[x0] // a
ldr w9,[x0,#4] // b
ldr w10,[x0,#8] // c
ldr w11,[x0,#12] // d
 
mov x6,#0 // indice t
/* x2 address begin each block */
ldr x1,qAdrsZoneTrav
add x2,x1,x7,lsl #6 // compute block begin indice * 4 * 16
6: // begin loop one
cmp x6,15
bgt 7f
// cas 1 f := (b et c) ou ((non b) et d)
// g := i
and w12,w9,w10
mvn w13,w9
and w13,w13,w11
orr w12,w12,w13 // f
mov x14,x6 // g
b 10f
7:
cmp x6,31
bgt 8f
// f := (d et b) ou ((non d) et c)
// g := (5×i + 1) mod 16
and w12,w11,w9
mvn w13,w11
and w13,w13,w10
orr w12,w12,w13 // f
mov x13,5
mul x13,x6,x13
add x13,x13,1
lsr x15,x13,4
lsl x15,x15,4
sub x14,x13,x15
b 10f
8:
cmp x6,47
bgt 9f
// f := b xor c xor d
// g := (3×i + 5) mod 16
eor w12,w9,w10
eor w12,w12,w11
mov x13,3
mul x13,x6,x13
add x13,x13,5
lsr x15,x13,4
lsl x15,x15,4
sub x14,x13,x15
b 10f
9:
// f := c xor (b ou (non d))
// g := (7×i) mod 16
mvn w13,w11
orr w13,w13,w9
eor w12,w13,w10 // f
mov x13,7
mul x13,x6,x13
lsr x15,x13,4
lsl x15,x15,4
sub x14,x13,x15 // g
 
10:
mov w15,w11
mov w11,w10 // d = c
mov w10,w9 // c = b
add w16,w8,w12 // a + f
ldr w17,[x2,x14,lsl #2]
add w16,w16,w17 // + valeur bloc g
ldr w13,[x3,x6,lsl #2]
add w16,w16,w13 // + valeur constante K de i
ldr w17,[x5,x6,lsl #2] // rotate left value
mov w13,32
sub w17,w13,w17
ror w13,w16,w17
add w9,w9,w13 // new b
mov w8, w15 // new a
add x6,x6,1
cmp x6,63
ble 6b
ldr x0,qAdrtbH
ldr w1,[x0] // H0
add w1,w1,w8 // + a
str w1,[x0]
ldr w1,[x0,#4] // H1
add w1,w1,w9 // + b
str w1,[x0,#4]
ldr w1,[x0,#8] // H2
add w1,w1,w10 // + c
str w1,[x0,#8]
ldr w1,[x0,#12] // H3
add w1,w1,w11 // + d
str w1,[x0,#12]
// other bloc
add x7,x7,1 // increment block
cmp x7,x4 // maxi ?
ble 5b
// compute final result
ldr x0,qAdrtbH // start area H
ldr x2,qAdrsZoneResult
ldr w1,[x0]
str w1,[x2]
ldr w1,[x0,#4]
str w1,[x2,#4]
ldr w1,[x0,#8]
str w1,[x2,#8]
ldr w1,[x0,#12]
str w1,[x2,#12]
mov x0,#0 // routine OK
b 100f
99: // size error
ldr x0,qAdrszMessSizeError
bl affichageMess
mov x0,-1
100:
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
qAdrtbConstH: .quad tbConstH
qAdrtbConstK: .quad tbConstK
qAdrtbRotaR: .quad tbRotaR
qAdrtbH: .quad tbH
qAdrsZoneResult: .quad sZoneResult
qAdrszMessSizeError: .quad szMessSizeError
 
/******************************************************************/
/* display hash MD5 */
/******************************************************************/
/* x0 contains the address of hash */
displayMD5:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x3,x0
mov x2,#0
1:
ldr w0,[x3,x2,lsl #2] // load 4 bytes
rev w0,w0 // reverse bytes
ldr x1,qAdrsZoneConv
bl conversion16_4W // conversion hexa
ldr x0,qAdrsZoneConv
bl affichageMess
add x2,x2,#1
cmp x2,#MD5_DIGEST_LENGTH / 4
blt 1b // and loop
ldr x0,qAdrszCarriageReturn
bl affichageMess // display message
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* conversion hexadecimal register 32 bits */
/******************************************************************/
/* x0 contains value and x1 address zone receptrice */
conversion16_4W:
stp x0,lr,[sp,-48]! // save registres
stp x1,x2,[sp,32] // save registres
stp x3,x4,[sp,16] // save registres
mov x2,#28 // start bit position
mov x4,#0xF0000000 // mask
mov x3,x0 // save entry value
1: // start loop
and x0,x3,x4 // value register and mask
lsr x0,x0,x2 // right shift
cmp x0,#10 // >= 10 ?
bge 2f // yes
add x0,x0,#48 // no is digit
b 3f
2:
add x0,x0,#55 // else is a letter A-F
3:
strb w0,[x1],#1 // load result and + 1 in address
lsr x4,x4,#4 // shift mask 4 bits left
subs x2,x2,#4 // decrement counter 4 bits <= zero ?
bge 1b // no -> loop
100: // fin standard de la fonction
ldp x3,x4,[sp,16] // restaur des 2 registres
ldp x1,x2,[sp,32] // restaur des 2 registres
ldp x0,lr,[sp],48 // restaur des 2 registres
ret
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
 
</syntaxhighlight>
<pre>
Result for => D41D8CD98F00B204E9800998ECF8427E
Result for abc => 900150983CD24FB0D6963F7D28E17F72
Result for abcdefghijklmnopqrstuvwxyz => C3FCD3D76192E4007DFB496CCA67E13B
Result for 12345678901234567890123456789012345678901234567890123456789012345678901234567890 => 57EDF4A22BE3C955AC49DA2E2107B67A
Result for ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 => D174AB98D277D9F5A5611C2C9F419D9F
Program End ok.
</pre>
=={{header|Ada}}==
{{works with|GNAT}}
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with GNAT.MD5;
 
Line 32 ⟶ 423:
begin
Put(GNAT.MD5.Digest("Foo bar baz"));
end MD5_Digest;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">
 
# Based on wikipedia article pseudocode #
Line 187 ⟶ 578:
print (("failed"))
FI
</syntaxhighlight>
</lang>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">
md5←{
⍝ index origin zero
⎕IO←0
⍝ decoding UTF-8 & padding
M←(⊢,(0⍴⍨512|448-512|≢))1,⍨l←,⍉(8⍴2)⊤'UTF-8'⎕UCS ⍵
⍝ add length
M,←,⍉(8⍴2)⊤⌽(8⍴256)⊤≢l
⍝ init registers
A←16⊥6 7 4 5 2 3 0 1
B←16⊥14 15 12 13 10 11 8 9
C←16⊥9 8 11 10 13 12 15 14
D←16⊥1 0 3 2 5 4 7 6
⍝ T table
T←⌊(2*32)×|1○1+⍳64
⍝ index table
K←16|i,(1+5×i),(5+3×i),7×i←⍳16
⍝ rot table
S←,1 0 2⍉4 4 4⍴7 12 17 22 5 9 14 20 4 11 16 23 6 10 15 21
⍝ truncate ⍵ to 32 bit & rot left ⍺
rot←{2⊥⍺⌽(32⍴2)⊤⍵}
proc←{
⍝ pack 512 bits into 32 bit words &
⍝ precompute X[k] + T[i]
l←T+(⊂K)⌷256⊥⍉⌽16 4⍴2⊥⍉64 8⍴⍺
fn←{
⍝ a b c d to binary
a b c d←↓⍉(32⍴2)⊤⍵
⍝ a + F(b,c,d)
⍺<16:S[⍺]rot l[⍺]+2⊥a+d≠b∧c≠d
⍺<32:S[⍺]rot l[⍺]+2⊥a+(b∧d)∨(c∧~d)
⍺<48:S[⍺]rot l[⍺]+2⊥a+b≠c≠d
S[⍺]rot l[⍺]+2⊥a+c≠b∨~d
}
(2*32)|⍵+⊃{¯1⌽((⍵[1]+⍺ fn ⍵)@0)⍵}/(⌽⍳64),⊂⍵
}
⍝ process each 512 bits
loop←{⍬≡⍺:⍵ ⋄ (512↓⍺)∇(512↑⍺)proc ⍵}
⍝ output registers
(⎕D,⎕A)[,⍉(2⍴16)⊤,⍉⊖(4⍴256)⊤M loop A B C D]
}
</syntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print digest "The quick brown fox jumped over the lazy dog's back"</langsyntaxhighlight>
 
{{out}}
 
<pre>e38ca1d920c4b8b8d3946b2c72f01680</pre>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program MD5.s */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
 
.equ LGHASH, 16 // result length
.equ ZWORKSIZE, 1000 // work area size
 
/*******************************************/
/* Structures */
/********************************************/
/* example structure variables */
.struct 0
var_a: // a
.struct var_a + 4
var_b: // b
.struct var_b + 4
var_c: // c
.struct var_c + 4
var_d: // d
.struct var_d + 4
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessTest1: .asciz "abc"
szMessTest4: .asciz "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
szMessTest2: .asciz "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
szMessTest3: .asciz "abcdefghijklmnopqrstuvwxyz"
szMessFinPgm: .asciz "Program End ok.\n"
szMessResult: .asciz "Result for "
szMessResult1: .asciz " => "
szMessSizeError: .asciz "\033[31mWork area too small !! \033[0m \n"
szCarriageReturn: .asciz "\n"
 
.align 4
/* array constantes K */
tbConstK: .int 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee
.int 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501
.int 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be
.int 0x6b901122,0xfd987193,0xa679438e,0x49b40821
.int 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa
.int 0xd62f105d,0x2441453,0xd8a1e681,0xe7d3fbc8
.int 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed
.int 0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a
.int 0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c
.int 0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70
.int 0x289b7ec6,0xeaa127fa,0xd4ef3085,0x4881d05
.int 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665
.int 0xf4292244,0x432aff97,0xab9423a7,0xfc93a039
.int 0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1
.int 0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1
.int 0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391
 
/* array rotation coef R */
tbRotaR: .int 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22
.int 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20
.int 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23
.int 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
 
tbConstH: .int 0x67452301 // H0
.int 0xEFCDAB89 // H1
.int 0x98BADCFE // H2
.int 0x10325476 // H3
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
sZoneConv: .skip 24
tbH: .skip 4 * 4 @ 4 variables H
tbabcd: .skip 4 * 4 @ 4 variables a b c d
sZoneTrav: .skip 1000
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
ldr r0,iAdrszMessTest1
bl computeExemple
ldr r0,iAdrszMessTest2
bl computeExemple
ldr r0,iAdrszMessTest3
bl computeExemple
ldr r0,iAdrszMessTest4
bl computeExemple
 
ldr r0,iAdrszMessFinPgm
bl affichageMess @ display message
 
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessResult: .int szMessResult
iAdrszMessResult1: .int szMessResult1
iAdrszMessTest1: .int szMessTest1
iAdrszMessTest2: .int szMessTest2
iAdrszMessTest3: .int szMessTest3
iAdrszMessTest4: .int szMessTest4
iAdrsZoneTrav: .int sZoneTrav
iAdrsZoneConv: .int sZoneConv
iAdrszMessFinPgm: .int szMessFinPgm
iAdrszMessSizeError: .int szMessSizeError
/***********************************************/
/* compute exemple */
/***********************************************/
/* r0 contains the address of the message */
computeExemple:
push {r1,lr} @ save registres
mov r1,r0
bl computeMD5 @ call routine MD5
ldr r0,iAdrszMessResult
bl affichageMess
mov r0,r1
bl affichageMess
ldr r0,iAdrszMessResult1
bl affichageMess
ldr r0, iAdrtbH
bl displayMD5
100:
pop {r1,pc} @ restaur registers
 
/******************************************************************/
/* compute MD5 */
/******************************************************************/
/* r0 contains the address of the message */
computeMD5:
push {r1-r12,lr} @ save registres
ldr r1,iAdrsZoneTrav
mov r2,#0 @ counter length
1: @ copy string in work area
cmp r2,#ZWORKSIZE @ maxi ?
bge 99f @ error
ldrb r3,[r0,r2]
strb r3,[r1,r2]
cmp r3,#0
addne r2,r2,#1
bne 1b
lsl r6,r2,#3 @ initial message length in bits
mov r3,#0b10000000 @ add bit 1 at end of string
strb r3,[r1,r2]
add r2,r2,#1 @ length in bytes
lsl r4,r2,#3 @ length in bits
mov r3,#0
2:
lsr r5,r2,#6
lsl r5,r5,#6
sub r5,r2,r5
cmp r5,#56
beq 3f @ yes -> end add
strb r3,[r1,r2] @ add zero at message end
add r2,#1 @ increment lenght bytes
add r4,#8 @ increment length in bits
b 2b
3:
str r6,[r1,r2] @ and store length at end
add r5,r2,#4
str r3,[r1,r5] @ store zero in hight bits for 64 bits
 
ldr r7,iAdrtbConstH @ constantes H address
ldr r4,iAdrtbH @ start area H
mov r5,#0
4: @ init array H with start constantes
ldr r6,[r7,r5,lsl #2] @ load constante
str r6,[r4,r5,lsl #2] @ and store
add r5,r5,#1
cmp r5,#4
blt 4b
@ split into block of 64 bytes
add r2,#4 @ TODO : à revoir
lsr r4,r2,#6 @ blocks number
 
ldr r0,iAdrtbH @ variables H
ldr r1,iAdrsZoneTrav
ldr r5,iAdrtbConstK
ldr r3,iAdrtbRotaR
ldr r8,iAdrtbabcd
mov r7,#0 @ n° de block et r1 contient l adresse zone de travail
5: @ begin loop of each block of 64 bytes
add r2,r1,r7,lsl #6 @ compute block begin indice * 4 * 16
mov r6,#0 @ indice t
/* COMPUTING THE MESSAGE DIGEST */
/* r0 variables H address */
/* r1 work area */
/* r2 block work area begin address */
/* r3 address constantes rotate */
/* r4 block number */
/* r5 constance K address */
/* r6 counter t */
/* r7 block counter */
/* r8 addresse variables a b c d */
@ init variable a b c d with variables H
mov r10,#0
6: @ loop init
ldr r9,[r0,r10,lsl #2] @ variables H
str r9,[r8,r10,lsl #2] @ variables a b c d
add r10,r10,#1
cmp r10,#4
blt 6b
7: @ loop begin
cmp r6,#15
bgt 8f
@ cas 1 f := (b et c) ou ((non b) et d)
@ g := i
ldr r9,[r8,#var_b]
ldr r10,[r8,#var_c]
and r12,r10,r9
mvn r9,r9
ldr r10,[r8,#var_d]
and r11,r9,r10
orr r12,r12,r11 @ f
mov r9,r6 @ g
b 11f
8:
cmp r6,#31
bgt 9f
@ f := (d et b) ou ((non d) et c)
@ g := (5×i + 1) mod 16
ldr r9,[r8,#var_b]
ldr r10,[r8,#var_d]
and r12,r10,r9
mvn r10,r10
ldr r9,[r8,#var_c]
and r11,r9,r10
orr r12,r12,r11 @ f
mov r9,#5
mul r9,r6,r9
add r9,r9,#1
lsr r10,r9,#4
lsl r10,r10,#4
sub r9,r9,r10 @ g
b 11f
9:
cmp r6,#47
bgt 10f
@ f := b xor c xor d
@ g := (3×i + 5) mod 16
ldr r9,[r8,#var_b]
ldr r10,[r8,#var_c]
eor r12,r10,r9
ldr r10,[r8,#var_d]
eor r12,r12,r10 @ f
mov r9,#3
mul r9,r6,r9
add r9,r9,#5
lsr r10,r9,#4
lsl r10,r10,#4
sub r9,r9,r10 @ g
b 11f
10:
@ f := c xor (b ou (non d))
@ g := (7×i) mod 16
ldr r10,[r8,#var_d]
mvn r12,r10
ldr r10,[r8,#var_b]
orr r12,r12,r10
ldr r10,[r8,#var_c]
eor r12,r12,r10 @ f
mov r9,#7
mul r9,r6,r9
lsr r10,r9,#4
lsl r10,r10,#4
sub r9,r9,r10 @ g
11:
ldr r10,[r8,#var_d]
mov r11,r10 @ save old d
ldr r10,[r8,#var_c]
str r10,[r8,#var_d] @ new d = c
ldr r10,[r8,#var_b]
str r10,[r8,#var_c] @ new c = b
ldr r10,[r8,#var_a]
add r12,r12,r10 @ a + f
ldr r10,[r2,r9,lsl #2]
add r12,r12,r10 @ + valeur bloc g
ldr r10,[r5,r6,lsl #2]
add r12,r12,r10 @ + valeur constante K de i
ldr r10,[r3,r6,lsl #2] @ rotate left value
rsb r10,r10,#32 @ compute right rotate
ror r12,r12,r10
ldr r10,[r8,#var_b]
add r12,r12,r10
str r12,[r8,#var_b] @ new b
str r11,[r8,#var_a] @ new a = old d
add r6,r6,#1
cmp r6,#63
ble 7b
@ maj area H
ldr r10,[r0] @ H0
ldr r11,[r8,#var_a]
add r10,r10,r11 @ + a
str r10,[r0]
ldr r10,[r0,#4] @ H1
ldr r11,[r8,#var_b]
add r10,r10,r11 @ + b
str r10,[r0,#4]
ldr r10,[r0,#8] @ H2
ldr r11,[r8,#var_c]
add r10,r10,r11 @ + c
str r10,[r0,#8]
ldr r10,[r0,#12] @ H3
ldr r11,[r8,#var_d]
add r10,r10,r11 @ + d
str r10,[r0,#12]
@ loop other block
add r7,r7,#1 @ increment block
cmp r7,r4 @ maxi ?
ble 5b
mov r9,#0 @ reverse bytes loop
12:
ldr r10,[r0,r9,lsl #2]
rev r10,r10 @ reverse bytes
str r10,[r0,r9,lsl #2]
add r9,r9,#1
cmp r9,#LGHASH / 4
blt 12b
mov r0,#0 @ routine OK
b 100f
99: @ size error
ldr r0,iAdrszMessSizeError
bl affichageMess
mov r0,#-1 @ error routine
100:
pop {r1-r12,pc} @ restaur registers
iAdrtbConstH: .int tbConstH
iAdrtbConstK: .int tbConstK
iAdrtbRotaR: .int tbRotaR
iAdrtbH: .int tbH
iAdrtbabcd: .int tbabcd
 
/*************************************************/
/* display hash MD5 */
/*************************************************/
/* r0 contains the address of hash */
displayMD5:
push {r1-r3,lr} @ save registres
mov r3,r0
mov r2,#0
1:
ldr r0,[r3,r2,lsl #2] @ load 4 bytes
ldr r1,iAdrsZoneConv
bl conversion16 @ conversion hexa
ldr r0,iAdrsZoneConv
bl affichageMess
add r2,r2,#1
cmp r2,#LGHASH / 4
blt 1b @ and loop
ldr r0,iAdrszCarriageReturn
bl affichageMess @ display message
100:
pop {r1-r3,lr} @ restaur registers
bx lr @ return
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
Result for abc => 900150983CD24FB0D6963F7D28E17F72
Result for ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 => D174AB98D277D9F5A5611C2C9F419D9F
Result for abcdefghijklmnopqrstuvwxyz => C3FCD3D76192E4007DFB496CCA67E13B
Result for 12345678901234567890123456789012345678901234567890123456789012345678901234567890 => 57EDF4A22BE3C955AC49DA2E2107B67A
Program End ok.
</pre>
=={{header|AutoHotkey}}==
{{AutoHotkey case}}
===Regular version===
Source: [http://www.autohotkey.com/forum/post-275910.html#275910 AutoHotkey forum] by SKAN
<langsyntaxhighlight lang="autohotkey">data := "abc"
MsgBox % MD5(data,StrLen(data)) ; 900150983cd24fb0d6963f7d28e17f72
 
Line 212 ⟶ 1,040:
Return MD5
}
</syntaxhighlight>
</lang>
 
===Native implementation===
Source: [http://www.autohotkey.com/forum/topic17853.html AutoHotkey forum] by Laszlo
<langsyntaxhighlight lang="autohotkey">; GLOBAL CONSTANTS r[64], k[64]
r = 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22
, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20
Line 290 ⟶ 1,118:
SetFormat Integer, DECIMAL
Return x
}</langsyntaxhighlight>
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="freebasic">
PRAGMA INCLUDE <stdio.h>
PRAGMA INCLUDE <stdlib.h>
Line 310 ⟶ 1,138:
FOR i = 0 TO MD5_DIGEST_LENGTH-1
PRINT result[i] FORMAT "%02x"
NEXT</langsyntaxhighlight>
 
 
=={{header|BASIC256}}==
<langsyntaxhighlight BASIC256lang="basic256">print MD5("")
print MD5("a")
print MD5("abc")
Line 321 ⟶ 1,149:
print MD5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
print MD5("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
end</langsyntaxhighlight>
{{out}}
<pre>d41d8cd98f00b204e9800998ecf8427e
Line 335 ⟶ 1,163:
{{works with|BBC BASIC for Windows}}
See [[MD5/Implementation]] for a native version.
<langsyntaxhighlight lang="bbcbasic"> PRINT FN_MD5("")
PRINT FN_MD5("a")
PRINT FN_MD5("abc")
Line 353 ⟶ 1,181:
MD5$ += RIGHT$("0"+STR$~(MD5_CTX.digest&(I%)),2)
NEXT
= MD5$</langsyntaxhighlight>
 
=={{header|C}}==
{{libheader|OpenSSL}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 377 ⟶ 1,205:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
Implementation of md5
(Needs review - differences observed for the last 8 characters when compared with openssl implementation)
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Line 521 ⟶ 1,349:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System.Text;
using System.Security.Cryptography;
 
byte[] data = Encoding.ASCII.GetBytes("The quick brown fox jumped over the lazy dog's back");
byte[] hash = MD5.Create().ComputeHash(data);
Console.WriteLine(BitConverter.ToString(hash).Replace("-", "").ToLower());</langsyntaxhighlight>
 
=={{header|C++}}==
{{libheader|Poco Crypto}}
<langsyntaxhighlight lang="cpp">#include <string>
#include <iostream>
#include "Poco/MD5Engine.h"
Line 552 ⟶ 1,380:
<< " !" << std::endl ;
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>The quick brown fox jumped over the lazy dog's back as a MD5 digest :
Line 565 ⟶ 1,393:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(apply str
(map (partial format "%02x")
(.digest (doto (java.security.MessageDigest/getInstance "MD5")
.reset
(.update (.getBytes "The quick brown fox jumps over the lazy dog"))))))</langsyntaxhighlight>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
<lang COBOL>
IDENTIFICATION DIVISION.
PROGRAM-ID. MD5.
Line 635 ⟶ 1,463:
CLOSE Tmp-MD5.
CALL "CBL_DELETE_FILE" USING '/tmp/MD5'.
</syntaxhighlight>
</lang>
<syntaxhighlight lang="cobol">
IDENTIFICATION DIVISION.
PROGRAM-ID. MD5-DEMO.
AUTHOR. Bill Gunshannon
INSTALLATION. Home.
DATE-WRITTEN. 16 December 2021.
************************************************************
** Program Abstract:
** Use the md5sum utility and pass the HASH back using
** a temp file. Not elegant, but it works.
** Same program but made MD5 a User Defined Function
** instead of a procedure.
************************************************************
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
 
REPOSITORY.
FUNCTION MD5.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Eof PIC X VALUE 'F'.
01 Str1.
05 Pre-cmd PIC X(8)
VALUE 'echo -n '.
05 Str1-complete.
10 Str1-Part1 PIC X(26)
VALUE 'The quick brown fox jumps'.
10 Str1-Part2 PIC X(19)
VALUE ' over the lazy dog'.
05 Post-cmd PIC X(20)
VALUE ' | md5sum > /tmp/MD5'.
01 Str1-MD5 PIC X(32).
PROCEDURE DIVISION.
Main-Program.
DISPLAY Str1-complete.
* PERFORM Get-MD5.
MOVE FUNCTION MD5(Str1) TO Str1-MD5.
DISPLAY Str1-MD5.
STOP RUN.
 
END PROGRAM MD5-DEMO.
IDENTIFICATION DIVISION.
FUNCTION-ID. MD5.
 
ENVIRONMENT DIVISION.
 
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT Tmp-MD5 ASSIGN TO "/tmp/MD5"
ORGANIZATION IS LINE SEQUENTIAL.
 
 
DATA DIVISION.
 
FILE SECTION.
 
FD Tmp-MD5
DATA RECORD IS MD5-Rec.
01 MD5-Rec PIC X(32).
 
 
 
LINKAGE SECTION.
01 Str1 PIC X(128).
01 Str1-MD5 PIC X(32).
 
PROCEDURE DIVISION USING Str1 RETURNING Str1-MD5.
CALL "SYSTEM" USING FUNCTION TRIM(Str1).
OPEN INPUT Tmp-MD5.
READ Tmp-MD5 INTO Str1-MD5.
CLOSE Tmp-MD5.
CALL "CBL_DELETE_FILE" USING '/tmp/MD5'.
GO-BACK.
 
END FUNCTION MD5.
</syntaxhighlight>
{{Out}}
<pre>
Line 644 ⟶ 1,562:
=={{header|Common Lisp}}==
{{libheader|Ironclad}}
<langsyntaxhighlight lang="lisp">(ql:quickload 'ironclad)
(defun md5 (str)
(ironclad:byte-array-to-hex-string
Line 658 ⟶ 1,576:
(dolist (msg *tests*)
(format T "~s: ~a~%" msg (md5 msg)))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 670 ⟶ 1,588:
 
<h5>Another method using openssl:</h5> {{libheader|CFFI}}
<langsyntaxhighlight lang="lisp">(cffi:load-foreign-library "libcrypto.so")
 
(cffi:defcfun ("MD5" MD5) :void (string :string) (len :int) (ptr :pointer))
Line 679 ⟶ 1,597:
(loop for i from 0 below 16 do
(format t "~a" (write-to-string (cffi:mem-ref ptr :unsigned-char i) :base 16)))
(cffi:foreign-free ptr))</langsyntaxhighlight>
 
{{Out}}<pre>E38CA1D920C4B8B8D3946B2C72F01680</pre>
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">require "digest/md5"
 
puts Digest::MD5.hexdigest("The quick brown fox jumped over the lazy dog's back")</langsyntaxhighlight>
{{Out}}<pre>e38ca1d920c4b8b8d3946b2c72f01680</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.digest.md;
 
auto txt = "The quick brown fox jumped over the lazy dog's back";
writefln("%-(%02x%)", txt.md5Of);
}</langsyntaxhighlight>
{{out}}
<pre>e38ca1d920c4b8b8d3946b2c72f01680</pre>
Alternative version:
{{libheader|Tango}}
<langsyntaxhighlight lang="d">import tango.io.digest.Md5, tango.io.Stdout;
 
void main(char[][] args) {
Line 708 ⟶ 1,626:
Stdout.formatln("[{}]=>\n[{}]", args[i], md5.hexDigest());
}
}</langsyntaxhighlight>
Output:
<pre>>md5test "The quick brown fox jumped over the lazy dog's back"
Line 716 ⟶ 1,634:
=={{header|Delphi}}==
If you require a native implementation, look inside the class '''TIdHashMessageDigest5'''. This class is placed in the unit '''IdHashMessageDigest.pas'''.
<langsyntaxhighlight Delphilang="delphi">program MD5Hash;
 
{$APPTYPE CONSOLE}
Line 742 ⟶ 1,660:
Writeln(MD5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'));
Readln;
end.</langsyntaxhighlight>
'''Output:'''
<pre>
Line 757 ⟶ 1,675:
{{works with|E-on-Java}}
{{trans|Java}} (with modifications)
<langsyntaxhighlight lang="e">def makeMessageDigest := <import:java.security.makeMessageDigest>
def sprintf := <import:java.lang.makeString>.format
 
Line 766 ⟶ 1,684:
print(sprintf("%02x", [b]))
}
println()</langsyntaxhighlight>
 
=={{header|Emacs LispEasyLang}}==
<syntaxhighlight>
len md5k[] 64
proc md5init . .
for i = 1 to 64
md5k[i] = floor (0x100000000 * abs sin (i * 180 / pi))
.
.
md5init
#
func$ md5 inp$ .
subr addinp
if inp4 = 1
inp[] &= 0
.
inp[len inp[]] += b * inp4
inp4 *= 0x100
if inp4 = 0x100000000
inp4 = 1
.
.
s[] = [ 7 12 17 22 7 12 17 22 7 12 17 22 7 12 17 22 5 9 14 20 5 9 14 20 5 9 14 20 5 9 14 20 4 11 16 23 4 11 16 23 4 11 16 23 4 11 16 23 6 10 15 21 6 10 15 21 6 10 15 21 6 10 15 21 ]
inp[] = [ ]
inp4 = 1
for i = 1 to len inp$
b = strcode substr inp$ i 1
addinp
.
b = 0x80
addinp
while len inp[] mod 16 <> 14 or inp4 <> 1
b = 0
addinp
.
h = len inp$ * 8
for i = 1 to 4
b = h mod 0x100
addinp
h = h div 0x100
.
inp[] &= 0
#
a0 = 0x67452301
b0 = 0xefcdab89
c0 = 0x98badcfe
d0 = 0x10325476
for chunk = 1 step 16 to len inp[] - 15
a = a0 ; b = b0 ; c = c0 ; d = d0
for i = 1 to 64
if i <= 16
h1 = bitand b c
h2 = bitand bitnot b d
f = bitor h1 h2
g = i - 1
elif i <= 32
h1 = bitand d b
h2 = bitand bitnot d c
f = bitor h1 h2
g = (5 * i - 4) mod 16
elif i <= 48
h1 = bitxor b c
f = bitxor h1 d
g = (3 * i + 2) mod 16
else
h1 = bitor b bitnot d
f = bitxor c h1
g = (7 * i - 7) mod 16
.
f = (f + a + md5k[i] + inp[chunk + g])
a = d
d = c
c = b
h1 = bitshift f s[i]
h2 = bitshift f (s[i] - 32)
b = (b + h1 + h2)
.
a0 += a ; b0 += b ; c0 += c ; d0 += d
.
for a in [ a0 b0 c0 d0 ]
for i = 1 to 4
b = a mod 256
a = a div 256
for h in [ b div 16 b mod 16 ]
h += 48
if h > 57
h += 39
.
s$ &= strchar h
.
.
.
return s$
.
#
print md5 "The quick brown fox jumped over the lazy dog's back"
</syntaxhighlight>
{{out}}
<pre>
e38ca1d920c4b8b8d3946b2c72f01680
</pre>
 
=={{header|Emacs Lisp}}==
Emacs 23 and up includes <code>md5</code> giving the MD5 hash as a hexadecimal string. (See [http://www.gnu.org/software/emacs/manual/html_node/elisp/Checksum_002fHash.html GNU Elisp manual on Checksum/Hash]).
 
<lang Lisp>(md5 "The quick brown fox jumped over the lazy dog's back")
=>
"e38ca1d920c4b8b8d3946b2c72f01680"</lang>
 
This began in [http://git.chise.org/elisp/flim/ FLIM] and FLIM has an <code>md5.el</code> which creates an <code>md5</code> func if it doesn't already exist.
 
<code>hmac-md5.el</code> also from FLIM and also included in recent Emacs has an <code>md5-binary</code> giving the checksum as 16 binary bytes. <code>encode-hex-string</code> from <code>hex-util.el</code> can convert that to hex the same as the <code>md5</code> func gives.
 
<syntaxhighlight lang="lisp">(md5 "The quick brown fox jumped over the lazy dog's back") ;=> "e38ca1d920c4b8b8d3946b2c72f01680"
<lang Lisp>(require 'hmac-md5)
(secure-hash 'md5 "The quick brown fox jumped over the lazy dog's back") ;=> "e38ca1d920c4b8b8d3946b2c72f01680"</syntaxhighlight>
(require 'hex-util)
(encode-hex-string
(md5-binary "The quick brown fox jumped over the lazy dog's back"))
=>
"e38ca1d920c4b8b8d3946b2c72f01680"</lang>
 
=={{header|Erlang}}==
By default, Erlang's crypto functions like md5 return a binary value rather than a hex string. We have two write our own function to translate it:
<langsyntaxhighlight Erlanglang="erlang">-module(tests).
-export([md5/1]).
 
Line 795 ⟶ 1,800:
string:to_upper(
lists:flatten([io_lib:format("~2.16.0b",[N]) || <<N>> <= erlang:md5(S)])
).</langsyntaxhighlight>
Testing it:
<langsyntaxhighlight lang="erlang">1> c(tests).
{ok,tests}
2> tests:md5("The quick brown fox jumped over the lazy dog's back").
"E38CA1D920C4B8B8D3946B2C72F01680"</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Using built-in System.Security.Cryptography.MD5 class (Link to original blog [https://znprojects.blogspot.com/2017/04/md5-in-f-functionally.html]).
 
<langsyntaxhighlight Flang="f#">let md5ootb (msg: string) =
use md5 = System.Security.Cryptography.MD5.Create()
msg
Line 813 ⟶ 1,818:
|> Seq.reduce ( + )
 
md5ootb @"The quick brown fox jumped over the lazy dog's back"</langsyntaxhighlight>
 
=={{header|Factor}}==
Line 824 ⟶ 1,829:
=={{header|Forth}}==
{{libheader|Forth Foundation Library}}
<langsyntaxhighlight lang="forth">include ffl/md5.fs
 
\ Create a MD5 variable md1 in the dictionary
Line 841 ⟶ 1,846:
\ Convert the hash value to a hex string and print it
md5+to-string type cr</langsyntaxhighlight>
 
=={{header|Fortran}}==
Line 847 ⟶ 1,852:
Using Windows API. See [https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptacquirecontexta CryptAcquireContextA], [https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptcreatehash CryptCreateHash], [https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-crypthashdata CryptHashData] and [https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptgethashparam CryptGetHashParam] in Microsoft documentation.
 
<langsyntaxhighlight lang="fortran">module md5_mod
use kernel32
use advapi32
Line 940 ⟶ 1,945:
deallocate(name)
end do
end program</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
Line 947 ⟶ 1,952:
=={{header|Frink}}==
The function <CODE>messageDigest[string, hash]</CODE> returns a hex-encoded hash of any input string with a variety of hashing functions.
<langsyntaxhighlight lang="frink">println[messageDigest["The quick brown fox", "MD5"]]</langsyntaxhighlight>
 
=={{header|Futhark}}==
Line 953 ⟶ 1,958:
Real languages roll their own crypto.
 
<syntaxhighlight lang="futhark">
<lang Futhark>
type md5 = (u32, u32, u32, u32)
 
Line 1,029 ⟶ 2,034:
in (d, b + rotate_left(a + f + (ks())[i] + m[g], (rs())[i]), b, c)
in (a,b,c,d)
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,071 ⟶ 2,076:
fmt.Println(" got: ", sum)
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight Groovylang="groovy">import java.security.MessageDigest
 
String.metaClass.md5Checksum = {
MessageDigest.getInstance('md5').digest(delegate.bytes).collect { String.format("%02x", it) }.join('')
}</langsyntaxhighlight>
Testing
<langsyntaxhighlight Groovylang="groovy">assert 'The quick brown fox jumps over the lazy dog'.md5Checksum() == '9e107d9d372bb6826bd81d3542a419d6'</langsyntaxhighlight>
 
=={{header|Haskell}}==
Use modules nano-MD5 and ByteString from [http://hackage.haskell.org/packages/hackage.html HackageDB]
<langsyntaxhighlight Haskelllang="haskell">import Data.Digest.OpenSSL.MD5 (md5sum)
import Data.ByteString (pack)
import Data.Char (ord)
Line 1,091 ⟶ 2,096:
let message = "The quick brown fox jumped over the lazy dog's back"
digest = (md5sum . pack . map (fromIntegral . ord)) message
putStrLn digest</langsyntaxhighlight>
 
Use in GHCi:
Line 1,099 ⟶ 2,104:
This version uses the [https://hackage.haskell.org/package/cryptonite Cryptonite] package:
{{libheader|Cryptonite}}
<langsyntaxhighlight lang="haskell">#!/usr/bin/env runhaskell
 
import Data.ByteString.Char8 (pack)
Line 1,107 ⟶ 2,112:
main :: IO ()
main = print . md5 . pack . unwords =<< getArgs
where md5 x = hash x :: Digest MD5</langsyntaxhighlight>
{{out}}
<pre>
Line 1,115 ⟶ 2,120:
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">import haxe.crypto.Md5;
 
class Main {
Line 1,122 ⟶ 2,127:
Sys.println(md5);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,129 ⟶ 2,134:
=={{header|Icon}} and {{header|Unicon}}==
The following program demonstrates the MD5 using a native Icon/Unicon implementation (see [[MD5/Implementation]]) and checks the results against reference values. Alternate implementations using call outs to md5sum on Linux or fciv on windows are possible but were not coded.
<langsyntaxhighlight Iconlang="icon">procedure main() # validate against the RFC test strings and more
testMD5("The quick brown fox jumps over the lazy dog", 16r9e107d9d372bb6826bd81d3542a419d6)
testMD5("The quick brown fox jumps over the lazy dog.", 16re4d909c290d0fb1ca068ffaddf22cbd0)
Line 1,138 ⟶ 2,143:
write("Message(length=",*s,") = ",image(s))
write("Digest = ",hexstring(h := MD5(s)),if h = rh then " matches reference hash" else (" does not match reference hash = " || hexstring(rh)),"\n")
end</langsyntaxhighlight>
 
Sample Output:<pre>Message(length=43) = "The quick brown fox jumps over the lazy dog"
Line 1,150 ⟶ 2,155:
 
=={{header|Io}}==
<langsyntaxhighlight lang="io">Io> MD5
==> MD5_0x97663e0:
appendSeq = MD5_appendSeq()
Line 1,156 ⟶ 2,161:
md5String = MD5_md5String()
Io> MD5 clone appendSeq("The quick brown fox jumped over the lazy dog's back") md5String
==> e38ca1d920c4b8b8d3946b2c72f01680</langsyntaxhighlight>
 
=={{header|J}}==
Using the <tt>[httphttps://www.jsoftwaregithub.com/wsvn/addonsjsoftware/trunkconvert_misc/convertblob/miscmaster/md5.ijs md5]</tt> script from the <tt>convert/misc</tt> addon package:
<langsyntaxhighlight lang="j"> require 'convert/misc/md5'
md5 'The quick brown fox jumped over the lazy dog''s back'
e38ca1d920c4b8b8d3946b2c72f01680</langsyntaxhighlight>
 
Implementation here, in case the link to the script goes dead again:
<syntaxhighlight lang="j">require 'convert'
 
'`lt gt ge xor'=: (20 b.)`(18 b.)`(27 b.)`(22 b.)
'`and or sh'=: (17 b.)`(23 b.)`(33 b.)
 
{{
if. IF64 do.
rot=: (16bffffffff and sh or ] sh~ 32 -~ [) NB. (y << x) | (y >>> (32 - x))
add=: ((16bffffffff&and)@+)"0
else.
rot=: (32 b.)
add=: (+&(_16&sh) (16&sh@(+ _16&sh) or and&65535@]) +&(and&65535))"0
end.
EMPTY
}}''
 
hexlist=: tolower@:,@:hfd@:,@:(|."1)@(256 256 256 256&#:)
 
cmn=: {{
'x s t'=. x [ 'q a b'=. y
b add s rot (a add q) add (x add t)
}}
 
ff=: cmn (((1&{ and 2&{) or 1&{ lt 3&{) , 2&{.)
gg=: cmn (((1&{ and 3&{) or 2&{ gt 3&{) , 2&{.)
hh=: cmn (((1&{ xor 2&{)xor 3&{ ) , 2&{.)
ii=: cmn (( 2&{ xor 1&{ ge 3&{ ) , 2&{.)
op=: ff`gg`hh`ii
 
I=: ".;._2{{)n
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 6 11 0 5 10 15 4 9 14 3 8 13 2 7 12
5 8 11 14 1 4 7 10 13 0 3 6 9 12 15 2
0 7 14 5 12 3 10 1 8 15 6 13 4 11 2 9
}}
S=: 4 4$7 12 17 22 5 9 14 20 4 11 16 23 6 10 15 21
 
T=: |:".;._2{{)n
_680876936 _165796510 _378558 _198630844
_389564586 _1069501632 _2022574463 1126891415
606105819 643717713 1839030562 _1416354905
_1044525330 _373897302 _35309556 _57434055
_176418897 _701558691 _1530992060 1700485571
1200080426 38016083 1272893353 _1894986606
_1473231341 _660478335 _155497632 _1051523
_45705983 _405537848 _1094730640 _2054922799
1770035416 568446438 681279174 1873313359
_1958414417 _1019803690 _358537222 _30611744
_42063 _187363961 _722521979 _1560198380
_1990404162 1163531501 76029189 1309151649
1804603682 _1444681467 _640364487 _145523070
_40341101 _51403784 _421815835 _1120210379
_1502002290 1735328473 530742520 718787259
1236535329 _1926607734 _995338651 _343485551
}}
 
norm=: {{
N=. 16 * 1 + _6 sh 8 + #y
b=. N#0 [ y=. a.i.y
for_i. i. #y do.
b=. ((j { b) or (8*4|i) sh i{y) (j=. _2 sh i) } b
end.
b=. ((j { b) or (8*4|i) sh 128) (j=._2 sh i=.#y) } b
_16]\ (8 * #y) (N-2) } b
}}
 
md5=: {{
X=. norm y
q=. r=. 1732584193 _271733879 _1732584194 271733878
for_k. X do.
for_j. i.4 do.
l=. ((j{I){k) ,. (16$j{S) ,. j{T
for_i. i.16 do.
r=. _1|.((i{l) (op@.j) r),}.r
end.
end.
q=. r=. r add q
end.
hexlist r
}}</syntaxhighlight>
 
An alternative and faster approach is to use the Qt library function available using the <tt>ide/qt</tt> addon from J8:
<langsyntaxhighlight lang="j"> require '~addons/ide/qt/qt.ijs'
getmd5=: 'md5'&gethash_jqtide_
getmd5 'The quick brown fox jumped over the lazy dog''s back'
e38ca1d920c4b8b8d3946b2c72f01680</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Line 1,195 ⟶ 2,282:
}
}
}</langsyntaxhighlight>
 
Other options for digest algorithms (to replace "MD5" in the example above) include: MD2, SHA-1, SHA-256, SHA-384, and SHA-512.
Line 1,201 ⟶ 2,288:
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">/* MD5 hash in Jsish */
var str = 'Rosetta code';
puts(Util.hash(str, {type:'md5'}));
Line 1,227 ⟶ 2,314:
MD5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') ==> 57edf4a22be3c955ac49da2e2107b67a
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 1,249 ⟶ 2,336:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using Nettle
 
function Base.trunc(s::AbstractString, n::Integer)
Line 1,280 ⟶ 2,367:
println("* The sum should be ", h)
end
end</langsyntaxhighlight>
 
{{out}}
Line 1,294 ⟶ 2,381:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.security.MessageDigest
Line 1,305 ⟶ 2,392:
for (byte in digest) print("%02x".format(byte))
println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,313 ⟶ 2,400:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">Encrypt_MD5('Welcome all Rhinos!')
//80ba88ee2600e9e9b36e739458c39ebd</langsyntaxhighlight>
 
===Test suite===
<langsyntaxhighlight Lassolang="lasso">local(test = map(
'a' = '0cc175b9c0f1b6a831c399e269772661',
'abc' = '900150983cd24fb0d6963f7d28e17f72',
Line 1,328 ⟶ 2,415:
with a in #test->keys do => {^
'testing: "'+#a+'": '+(Encrypt_MD5(#a)->asBytes == #test->find(#a)->asBytes)+'\r'
^}</langsyntaxhighlight>
{{out}}
<pre>testing: "12345678901234567890123456789012345678901234567890123456789012345678901234567890": true
Line 1,338 ⟶ 2,425:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">'[RC]MD5
'from tsh73's January 2008 code
 
Line 1,482 ⟶ 2,569:
a$=left$("00000000", 8-len(a$))+a$
revOrd$ = lower$(mid$(a$,7,2)+mid$(a$,5,2)+mid$(a$,3,2)+mid$(a$,1,2))
end function</langsyntaxhighlight>
 
=={{header|Lingo}}==
Line 1,488 ⟶ 2,575:
*Using a binary plugin ("Xtra"):
{{libheader|Crypto Xtra}}
<syntaxhighlight lang ="lingo">put cx_md5_string(str)</langsyntaxhighlight>
 
*Pure Lingo implementation
<langsyntaxhighlight lang="lingo">----------------------------------------
-- Calculates MD5 hash of string or bytearray
-- @param {bytearray|string} input
Line 1,766 ⟶ 2,853:
ba.position = 1
return ba
end</langsyntaxhighlight>
 
=={{header|LiveCode}}==
Livecode has built-in support for md4 and md5
<langsyntaxhighlight LiveCodelang="livecode">function md5sum hashtext
local md5, mdhex
put md5Digest(hashtext) into md5
get binaryDecode("H*",md5,mdhex)
return mdhex
end md5sum</langsyntaxhighlight>
 
Tests<langsyntaxhighlight LiveCodelang="livecode">command md5testsuite
// rfc1321 MD5 test suite:
local md5
Line 1,800 ⟶ 2,887:
end repeat
put results
end md5testsuite</langsyntaxhighlight>
Output
<pre>ok abc
Line 1,812 ⟶ 2,899:
=={{header|Lua}}==
Using the [http://www.keplerproject.org/md5/ Kepler MD5 library]:
<langsyntaxhighlight Lualang="lua">require "md5"
 
--printing a sum:
Line 1,827 ⟶ 2,914:
test("abcdefghijklmnopqrstuvwxyz","c3fcd3d76192e4007dfb496cca67e13b")
test("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789","d174ab98d277d9f5a5611c2c9f419d9f")
test("12345678901234567890123456789012345678901234567890123456789012345678901234567890","57edf4a22be3c955ac49da2e2107b67a")</langsyntaxhighlight>
 
=={{header|Maple}}==
The <tt>Hash</tt> command in the <tt>StringTools</tt> package computes the MD5 hash value of a string.
<syntaxhighlight lang="maple">
<lang Maple>
> with( StringTools ):
> Hash( "" );
Line 1,853 ⟶ 2,940:
> Hash( "12345678901234567890123456789012345678901234567890123456789012345678901234567890" );
"57edf4a22be3c955ac49da2e2107b67a"
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Mathematica has built-in functions Hash and FileHash.
Example:
<langsyntaxhighlight Mathematicalang="mathematica"> Hash["The quick brown fox jumped over the lazy dog's back","MD5","HexString"]</langsyntaxhighlight>
gives back:
<syntaxhighlight lang Mathematica="mathematica"> e38ca1d920c4b8b8d3946b2c72f01680</langsyntaxhighlight>
 
=={{header|MATLAB}}==
This code also works with Octave (but Octave already provides md5sum(), see [[{{FULLPAGENAME}}#Octave|Octave example]]).
 
<langsyntaxhighlight MATLABlang="matlab">function digest = md5(message)
% digest = md5(message)
% Compute the MD5 digest of the message, as a hexadecimal digest.
Line 1,980 ⟶ 3,067:
digest = dec2hex(digest);
digest = reshape(transpose(digest), 1, numel(digest));
end %md5</langsyntaxhighlight>
 
Sample Usage:
<langsyntaxhighlight MATLABlang="matlab">octave:14> md5('Rosetta Code')
ans = CCA1BF66B09554E10F837838C3D3EFB1</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">"The quick brown fox jumps over the lazy dog" md5 puts!</langsyntaxhighlight>
 
=={{header|MOO}}==
<langsyntaxhighlight lang="moo">string = "The quick brown fox jumped over the lazy dog's back";
player:tell(string_hash(string));</langsyntaxhighlight>
 
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
MD5 in Neko
Tectonics:
Line 2,008 ⟶ 3,095:
 
/* Output in lowercase hex */
$print(base_encode(result, "0123456789abcdef"));</langsyntaxhighlight>
 
{{out}}
Line 2,017 ⟶ 3,104:
=={{header|Nemerle}}==
{{trans|C#}}
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
using System.Text;
Line 2,050 ⟶ 3,137:
Write($"$(IsValidMD5(test.Key, test.Value)) ");
}
}</langsyntaxhighlight>
Output:
<pre>True True True True True True True True</pre>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
 
options replace format comments java crossref savelog symbols binary
Line 2,114 ⟶ 3,201:
return digestSum
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 2,152 ⟶ 3,239:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">;; using the crypto module from http://www.newlisp.org/code/modules/crypto.lsp.html
;; (import native functions from the crypto library, provided by OpenSSL)
(module "crypto.lsp")
(crypto:md5 "The quick brown fox jumped over the lazy dog's back")</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import md5
 
echo toMD5("The quick brown fox jumped over the lazy dog's back")</langsyntaxhighlight>
 
{{out}}
Line 2,167 ⟶ 3,254:
=={{header|Oberon-2}}==
{{works with|oo2c}}{{libheader|crypto}}
<langsyntaxhighlight lang="oberon2">
MODULE MD5;
IMPORT
Line 2,185 ⟶ 3,272:
Out.String("MD5: ");Utils.PrintHex(str,0,h.size);Out.Ln
END MD5.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,193 ⟶ 3,280:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class MD5 {
function : Main(args : String[]) ~ Nil {
Line 2,201 ⟶ 3,288:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
{{works with|GNUstep}} only; not Cocoa
<langsyntaxhighlight lang="objc">NSString *myString = @"The quick brown fox jumped over the lazy dog's back";
NSData *digest = [[myString dataUsingEncoding:NSUTF8StringEncoding] md5Digest]; // or another encoding of your choosing
NSLog(@"%@", [digest hexadecimalRepresentation]);</langsyntaxhighlight>
 
{{works with|iPhone}}
{{works with|Mac OS X}}
<langsyntaxhighlight lang="objc">#import <CommonCrypto/CommonDigest.h>
 
NSString *myString = @"The quick brown fox jumped over the lazy dog's back";
Line 2,222 ⟶ 3,309:
}
NSLog(@"%@", hex);
}</langsyntaxhighlight>
 
{{works with|Mac OS X}} (need to include "libcrypto.dylib" framework)
<langsyntaxhighlight lang="objc">#include <openssl/md5.h>
 
NSString *myString = @"The quick brown fox jumped over the lazy dog's back";
Line 2,236 ⟶ 3,323:
}
NSLog(@"%@", hex);
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml"># Digest.to_hex(Digest.string "The quick brown fox jumped over the lazy dog's back") ;;
- : string = "e38ca1d920c4b8b8d3946b2c72f01680"</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">s = "The quick brown fox jumped over the lazy dog's back";
hash = md5sum(s, true);
disp(hash)</langsyntaxhighlight>
 
For an implementation of MD5, see [[{{FULLPAGENAME}}#MATLAB|MATLAB example]].
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(import (otus ffi))
(define libcrypto (load-dynamic-library "libcrypto.so.1.0.0"))
(define MD5 (libcrypto type-vptr "MD5" type-string fft-unsigned-long type-string))
</syntaxhighlight>
</lang>
{{Out}}
<langsyntaxhighlight lang="scheme">
(define (test str)
(define md5 "----------------")
Line 2,274 ⟶ 3,361:
(test "abcdefghijklmnopqrstuvwxyz")
(test "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
(test "12345678901234567890123456789012345678901234567890123456789012345678901234567890")</langsyntaxhighlight>
 
<pre>
Line 2,288 ⟶ 3,375:
=={{header|OpenEdge/Progress}}==
The MD5-DIGEST function is readily available, the output is passed thru HEX-ENCODE to convert the raw result to a hexadecimal string, this then needs to be passed thru STRING for display purposes.
<langsyntaxhighlight OpenEdgelang="openedge/Progressprogress">MESSAGE
1 STRING( HEX-ENCODE( MD5-DIGEST( "" ) ) ) SKIP
2 STRING( HEX-ENCODE( MD5-DIGEST( "a" ) ) ) SKIP
Line 2,296 ⟶ 3,383:
6 STRING( HEX-ENCODE( MD5-DIGEST( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ) ) ) SKIP
7 STRING( HEX-ENCODE( MD5-DIGEST( "12345678901234567890123456789012345678901234567890123456789012345678901234567890" ) ) )
VIEW-AS ALERT-BOX</langsyntaxhighlight>
 
Output:
Line 2,318 ⟶ 3,405:
 
Build a MD5 plugin using Linux system library and PARI's function interface. (Linux solution)
<langsyntaxhighlight Clang="c">#include <pari/pari.h>
#include <openssl/md5.h>
 
Line 2,344 ⟶ 3,431:
 
return strtoGENstr(hash);
}</langsyntaxhighlight>
 
Compile with: gcc -Wall -O2 -fPIC -shared md5.c -o libmd5.so -lcrypt -lpari
 
Load plugin from your home directory into PARI:
<langsyntaxhighlight lang="parigp">install("plug_md5", "s", "MD5", "~/libmd5.so");
 
MD5("The quick brown fox jumped over the lazy dog's back") </langsyntaxhighlight>
 
Output:
Line 2,357 ⟶ 3,444:
 
Here is an explicit implementation in the PARI/GP language:
<langsyntaxhighlight PARIgplang="parigp">md5( message ) = { my(
a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476,
s = [ 7, 12, 17, 22; 5, 9, 14, 20; 4, 11, 16, 23; 6, 10, 15, 21 ],
Line 2,379 ⟶ 3,466:
); \\ end forstep
Strprintf("%032x",fromdigits(Vecrev(digits(fromdigits([d,c,b,a],2^32),256),16),256))
}</langsyntaxhighlight>
Then:
<langsyntaxhighlight PARIgplang="parigp">gp > md5("")</langsyntaxhighlight>
<pre>
%2 = "d41d8cd98f00b204e9800998ecf8427e"
</pre>
<langsyntaxhighlight PARIgplang="parigp">gp > md5("The quick brown fox jumps over lazy dogs.")</langsyntaxhighlight>
<pre>
%3 = "4c57070257998981a0c7b7ec003b9d5c"
Line 2,392 ⟶ 3,479:
==Pascal==
Pascal has a built-in unit called md5. It can be used to get a digest both for a string and a file.
<syntaxhighlight lang="pascal">
<lang Pascal>
program GetMd5;
 
Line 2,404 ⟶ 3,491:
writeln(strEncrypted);
end.
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,414 ⟶ 3,501:
=={{header|Perl}}==
{{libheader|Digest::MD5}}
<langsyntaxhighlight lang="perl">use Digest::MD5 qw(md5_hex);
 
print md5_hex("The quick brown fox jumped over the lazy dog's back"), "\n";</langsyntaxhighlight>
 
The same in OO manner
<langsyntaxhighlight lang="perl">use Digest::MD5;
 
$md5 = Digest::MD5->new;
$md5->add("The quick brown fox jumped over the lazy dog's back");
print $md5->hexdigest, "\n";</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--(phixonline)-->
See [[MD5/Implementation#Phix]]
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">$string = "The quick brown fox jumped over the lazy dog's back";
echo md5( $string );</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
Using the openssl library (the 'native' function is only in the 64-bit
version available):
<langsyntaxhighlight PicoLisplang="picolisp">(let Str "The quick brown fox jumped over the lazy dog's back"
(pack
(mapcar '((B) (pad 2 (hex B)))
(native "libcrypto.so" "MD5" '(B . 16) Str (length Str) '(NIL (16))) ) ) )</langsyntaxhighlight>
Output:
<pre>-> "E38CA1D920C4B8B8D3946B2C72F01680"</pre>
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">import String;
import Crypto.MD5;
 
int main(){
write( string2hex( hash( "The quick brown fox jumped over the lazy dog's back" ) ) + "\n" );
}</langsyntaxhighlight>
 
=={{header|PowerShell}}==
{{trans|C#}}
<langsyntaxhighlight lang="powershell">$string = "The quick brown fox jumped over the lazy dog's back"
$data = [Text.Encoding]::UTF8.GetBytes($string)
$hash = [Security.Cryptography.MD5]::Create().ComputeHash($data)
([BitConverter]::ToString($hash) -replace '-').ToLower()</langsyntaxhighlight>
 
=={{header|PureBasic}}==
;Purebasic 5.x versions:
<langsyntaxhighlight lang="purebasic">UseMD5Fingerprint() ; register the MD5 fingerprint plugin
 
test$ = "The quick brown fox jumped over the lazy dog's back"
Line 2,465 ⟶ 3,553:
; Call StringFingerprint() function and display MD5 result in Debug window
Debug StringFingerprint(test$, #PB_Cipher_MD5)
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
Line 2,471 ⟶ 3,559:
 
;Python 3.x, 2.5 and later 2.x versions:
<langsyntaxhighlight lang="python">>>> import hashlib
>>> # RFC 1321 test suite:
>>> tests = (
Line 2,483 ⟶ 3,571:
>>> for text, golden in tests: assert hashlib.md5(text).hexdigest() == golden
 
>>> </langsyntaxhighlight>
 
;Python 2.5 and later:
<langsyntaxhighlight lang="python">>>> import hashlib
>>> print hashlib.md5("The quick brown fox jumped over the lazy dog's back").hexdigest()
e38ca1d920c4b8b8d3946b2c72f01680</langsyntaxhighlight>
 
;Pre-2.5; removed in 3.x:
<langsyntaxhighlight lang="python">>>> import md5
>>> print md5.md5("The quick brown fox jumped over the lazy dog's back").hexdigest()
e38ca1d920c4b8b8d3946b2c72f01680</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">library(digest)
hexdigest <- digest("The quick brown fox jumped over the lazy dog's back",
algo="md5", serialize=FALSE)</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require file/md5)
Line 2,512 ⟶ 3,600:
(md5 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
(md5 "12345678901234567890123456789012345678901234567890123456789012345678901234567890")
</syntaxhighlight>
</lang>
 
Output:
Line 2,529 ⟶ 3,617:
(formerly Perl 6)
Library [http://github.com/cosimo/perl6-digest-md5/ Digest::MD5]
<syntaxhighlight lang="raku" perl6line>use Digest::MD5;
say Digest::MD5.md5_hex: "The quick brown fox jumped over the lazy dog's back";</langsyntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight lang="rebol">>> checksum/method "The quick brown fox jumped over the lazy dog" 'md5
== #{08A008A01D498C404B0C30852B39D3B8}</langsyntaxhighlight>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program tests the MD5 procedure (below) as per a test suite the IETF RFC (1321).*/
msg.1 = /*─────MD5 test suite [from above doc].*/
msg.2 = 'a'
Line 2,650 ⟶ 3,738:
return .a(.Lr(right(d2c(_+c2d(w) +c2d(.h(x,y,z))+c2d(!.n)),4,'0'x),m),x)
.part4: procedure expose !.; parse arg w,x,y,z,n,m,_; n=n+1
return .a(.Lr(right(d2c(c2d(w) +c2d(.i(x,y,z))+c2d(!.n)+_),4,'0'x),m),x)</langsyntaxhighlight>
'''output''' &nbsp; when using the default (internal) inputs:
<pre>
Line 2,676 ⟶ 3,764:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
See MD5("my string!") + nl
# output : a83a049fbe50cf7334caa86bf16a3520
</syntaxhighlight>
</lang>
 
=={{header|RLaB}}==
Line 2,688 ⟶ 3,776:
calculated in RLaB is the same as the hash of the same string vector written to a file.
 
<langsyntaxhighlight RLaBlang="rlab">>> x = "The quick brown fox jumped over the lazy dog's back"
The quick brown fox jumped over the lazy dog's back
 
>> hash("md5", x)
e38ca1d920c4b8b8d3946b2c72f01680</langsyntaxhighlight>
 
=={{header|RPG}}==
Modified from [http://www.mysamplecode.com/2011/05/rpgle-generate-sha-1-hash-use.html]:
<langsyntaxhighlight lang="rpg">**FREE
Ctl-opt MAIN(Main);
Ctl-opt DFTACTGRP(*NO) ACTGRP(*NEW);
Line 2,784 ⟶ 3,872:
endfor;
return;
end-proc;</langsyntaxhighlight>
Note that this implementation converts the input from EBCDIC to ASCII before computing the hash.
 
Line 2,795 ⟶ 3,883:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'digest'
Digest::MD5.hexdigest("The quick brown fox jumped over the lazy dog's back")
# => "e38ca1d920c4b8b8d3946b2c72f01680"</langsyntaxhighlight>
 
=={{header|Rust}}==
Cargo.toml
<langsyntaxhighlight lang="toml">
[dependencies]
rust-crypto = "0.2"
</syntaxhighlight>
</lang>
 
src/main.rs
<langsyntaxhighlight lang="rust">
extern crate crypto;
 
Line 2,818 ⟶ 3,906:
println!("{}", sh.result_str());
}
</syntaxhighlight>
</lang>
 
=={{header|S-lang}}==
Support for MD5 and SHA-1 are included in the standard "chksum" library:
 
<langsyntaxhighlight Slang="s-lang">require("chksum");
print(md5sum("The quick brown fox jumped over the lazy dog's back"));</langsyntaxhighlight>
 
{{out}}
Line 2,830 ⟶ 3,918:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object RosettaMD5 extends App {
 
def MD5(s: String): String = {
Line 2,850 ⟶ 3,938:
import scala.compat.Platform.currentTime
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "msgdigest.s7i";
 
Line 2,859 ⟶ 3,947:
begin
writeln(hex(md5("The quick brown fox jumped over the lazy dog's back")));
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,868 ⟶ 3,956:
=={{header|Sidef}}==
{{libheader|Digest::MD5}}
<langsyntaxhighlight lang="ruby">var digest = frequire('Digest::MD5');
say digest.md5_hex("The quick brown fox jumped over the lazy dog's back");</langsyntaxhighlight>
 
The same in OO manner
<langsyntaxhighlight lang="ruby">var md5 = require('Digest::MD5').new;
md5.add("The quick brown fox jumped over the lazy dog's back");
say md5.hexdigest;</langsyntaxhighlight>
 
=={{header|Slate}}==
You must load the code in 'src/lib/md5.slate'.
<langsyntaxhighlight lang="slate">'The quick brown fox jumped over the lazy dog\'s back' md5String. "==> 'e38ca1d920c4b8b8d3946b2c72f01680'"</langsyntaxhighlight>
 
 
=={{header|Slope}}==
<syntaxhighlight lang="slope">(display (string->md5 "The quick brown fox jumped over the lazy dog's back"))</syntaxhighlight>
{{out}}
<pre>e38ca1d920c4b8b8d3946b2c72f01680</pre>
 
=={{header|Smalltalk}}==
Line 2,884 ⟶ 3,978:
 
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">PackageLoader fileInPackage: 'Digest' !
(MD5 hexDigestOf: 'The quick brown fox jumped over the lazy dog''s back') displayNl.</langsyntaxhighlight>
 
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">(MD5Stream hashValueOf: 'The quick brown fox jumped over the lazy dog''s back') printCR.</langsyntaxhighlight>
 
=={{header|SQL}}==
{{works with|MySQL}}
<langsyntaxhighlight lang="sql">SELECT MD5('The quick brown fox jumped over the lazy dog\'s back')</langsyntaxhighlight>
 
=={{header|Suneido}}==
<langsyntaxhighlight Suneidolang="suneido">Md5('The quick brown fox jumped over the lazy dog\'s back')</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{tcllib|md5}}
<langsyntaxhighlight lang="tcl">package require md5
puts [md5::md5 -hex "The quick brown fox jumped over the lazy dog's back"]
# ==> E38CA1D920C4B8B8D3946B2C72F01680</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 2,907 ⟶ 4,001:
 
[[GNU]] coreutils has ''md5sum'':
<langsyntaxhighlight lang="bash">echo -n "The quick brown fox jumped over the lazy dog's back" | md5sum</langsyntaxhighlight>
 
Several [[BSD]] systems have ''md5:''
<langsyntaxhighlight lang="bash">echo -n "The quick brown fox jumped over the lazy dog's back" | md5</langsyntaxhighlight>
 
{{libheader|OpenSSL}}
<langsyntaxhighlight lang="bash">echo -n "The quick brown fox jumped over the lazy dog's back" |
openssl md5 | sed 's/.*= //'</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|9.0+}}
<langsyntaxhighlight lang="vbnet">Imports System.Security.Cryptography
Imports System.Text
 
Line 2,943 ⟶ 4,037:
 
End Module
</syntaxhighlight>
</lang>
 
{{out}}
<pre>
AF397EA30996B22759740AC66452D47A
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import crypto.md5
fn main() {
for p in [
// RFC 1321 test cases
["d41d8cd98f00b204e9800998ecf8427e", ""],
["0cc175b9c0f1b6a831c399e269772661", "a"],
["900150983cd24fb0d6963f7d28e17f72", "abc"],
["f96b697d7cb7938d525a2f31aaf161d0", "message digest"],
["c3fcd3d76192e4007dfb496cca67e13b", "abcdefghijklmnopqrstuvwxyz"],
["d174ab98d277d9f5a5611c2c9f419d9f",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"],
["57edf4a22be3c955ac49da2e2107b67a", "12345678901234567890" +
"123456789012345678901234567890123456789012345678901234567890"],
// test case popular with other RC solutions
["e38ca1d920c4b8b8d3946b2c72f01680",
"The quick brown fox jumped over the lazy dog's back"],
] {
validate(p[0], p[1])
}
}
fn validate(check string, s string) {
sum := md5.hexhash(s)
if sum != check {
println("MD5 fail")
println(" for string, $s")
println(" expected: $check")
println(" got: $sum")
} else {
println('MD5 succeeded $s')
}
}</syntaxhighlight>
{{out}}
<pre>MD5 succeeded
MD5 succeeded a
MD5 succeeded abc
MD5 succeeded message digest
MD5 succeeded abcdefghijklmnopqrstuvwxyz
MD5 succeeded ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
MD5 succeeded 12345678901234567890123456789012345678901234567890123456789012345678901234567890
MD5 succeeded The quick brown fox jumped over the lazy dog's back
</pre>
 
Line 2,954 ⟶ 4,093:
{{libheader|Wren-fmt}}
This script uses a library implementation of MD5 (based on the implementation in the [[MD5/Implementation#Wren]] task) to process the Wikipedia examples.
<langsyntaxhighlight ecmascriptlang="wren">import "./crypto" for Md5
import "./fmt" for Fmt
 
var strings = [
Line 2,966 ⟶ 4,105:
var digest = Md5.digest(s)
Fmt.print("$s <== '$0s'", digest, s)
}</langsyntaxhighlight>
 
{{out}}
Line 2,977 ⟶ 4,116:
=={{header|zkl}}==
md5 is a built in utility
<langsyntaxhighlight lang="zkl">Utils.MD5.calc("message digest"); //-->"f96b697d7cb7938d525a2f31aaf161d0"
Utils.MD5.calc("abcdefghijklmnopqrstuvwxyz"); //-->"c3fcd3d76192e4007dfb496cca67e13b"</langsyntaxhighlight>
 
 
2,033

edits