MD5: Difference between revisions

6,034 bytes added ,  3 months ago
Added Easylang
(add task to arm assembly raspberry pi)
(Added Easylang)
 
(4 intermediate revisions by 4 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 26:
=={{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">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program MD5_64.s */
Line 406:
.include "../includeARM64.inc"
 
</syntaxhighlight>
</lang>
<pre>
Result for => D41D8CD98F00B204E9800998ECF8427E
Line 417:
=={{header|Ada}}==
{{works with|GNAT}}
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with GNAT.MD5;
 
Line 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 578:
print (("failed"))
FI
</syntaxhighlight>
</lang>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">
md5←{
⍝ index origin zero
Line 623:
(⎕D,⎕A)[,⍉(2⍴16)⊤,⍉⊖(4⍴256)⊤M loop A B C D]
}
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print digest "The quick brown fox jumped over the lazy dog's back"</langsyntaxhighlight>
 
{{out}}
Line 634:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program MD5.s */
Line 1,017:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
Result for abc => 900150983CD24FB0D6963F7D28E17F72
Line 1,029:
===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 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 1,118:
SetFormat Integer, DECIMAL
Return x
}</langsyntaxhighlight>
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="freebasic">
PRAGMA INCLUDE <stdio.h>
PRAGMA INCLUDE <stdlib.h>
Line 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 1,149:
print MD5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
print MD5("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
end</langsyntaxhighlight>
{{out}}
<pre>d41d8cd98f00b204e9800998ecf8427e
Line 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 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 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 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 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 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 1,463:
CLOSE Tmp-MD5.
CALL "CBL_DELETE_FILE" USING '/tmp/MD5'.
</syntaxhighlight>
</lang>
<syntaxhighlight lang="cobol">
<lang COBOL>
IDENTIFICATION DIVISION.
Line 1,553:
 
END FUNCTION MD5.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,562:
=={{header|Common Lisp}}==
{{libheader|Ironclad}}
<langsyntaxhighlight lang="lisp">(ql:quickload 'ironclad)
(defun md5 (str)
(ironclad:byte-array-to-hex-string
Line 1,576:
(dolist (msg *tests*)
(format T "~s: ~a~%" msg (md5 msg)))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 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 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 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 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 1,660:
Writeln(MD5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'));
Readln;
end.</langsyntaxhighlight>
'''Output:'''
<pre>
Line 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 1,684:
print(sprintf("%02x", [b]))
}
println()</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<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}}==
 
<langsyntaxhighlight Lisplang="lisp">(md5 "The quick brown fox jumped over the lazy dog's back") ;=> "e38ca1d920c4b8b8d3946b2c72f01680"
(secure-hash 'md5 "The quick brown fox jumped over the lazy dog's back") ;=> "e38ca1d920c4b8b8d3946b2c72f01680"</langsyntaxhighlight>
 
=={{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 1,699 ⟶ 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 1,717 ⟶ 1,818:
|> Seq.reduce ( + )
 
md5ootb @"The quick brown fox jumped over the lazy dog's back"</langsyntaxhighlight>
 
=={{header|Factor}}==
Line 1,728 ⟶ 1,829:
=={{header|Forth}}==
{{libheader|Forth Foundation Library}}
<langsyntaxhighlight lang="forth">include ffl/md5.fs
 
\ Create a MD5 variable md1 in the dictionary
Line 1,745 ⟶ 1,846:
\ Convert the hash value to a hex string and print it
md5+to-string type cr</langsyntaxhighlight>
 
=={{header|Fortran}}==
Line 1,751 ⟶ 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 1,844 ⟶ 1,945:
deallocate(name)
end do
end program</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
Line 1,851 ⟶ 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 1,857 ⟶ 1,958:
Real languages roll their own crypto.
 
<syntaxhighlight lang="futhark">
<lang Futhark>
type md5 = (u32, u32, u32, u32)
 
Line 1,933 ⟶ 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,975 ⟶ 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,995 ⟶ 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 2,003 ⟶ 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 2,011 ⟶ 2,112:
main :: IO ()
main = print . md5 . pack . unwords =<< getArgs
where md5 x = hash x :: Digest MD5</langsyntaxhighlight>
{{out}}
<pre>
Line 2,019 ⟶ 2,120:
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">import haxe.crypto.Md5;
 
class Main {
Line 2,026 ⟶ 2,127:
Sys.println(md5);
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,033 ⟶ 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 2,042 ⟶ 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 2,054 ⟶ 2,155:
 
=={{header|Io}}==
<langsyntaxhighlight lang="io">Io> MD5
==> MD5_0x97663e0:
appendSeq = MD5_appendSeq()
Line 2,060 ⟶ 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>[https://github.com/jsoftware/convert_misc/blob/master/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:
<langsyntaxhighlight lang="j">require 'convert'
 
'`lt gt ge xor'=: (20 b.)`(18 b.)`(27 b.)`(22 b.)
Line 2,148 ⟶ 2,249:
end.
hexlist r
}}</langsyntaxhighlight>
 
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 2,181 ⟶ 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 2,187 ⟶ 2,288:
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">/* MD5 hash in Jsish */
var str = 'Rosetta code';
puts(Util.hash(str, {type:'md5'}));
Line 2,213 ⟶ 2,314:
MD5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') ==> 57edf4a22be3c955ac49da2e2107b67a
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 2,235 ⟶ 2,336:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using Nettle
 
function Base.trunc(s::AbstractString, n::Integer)
Line 2,266 ⟶ 2,367:
println("* The sum should be ", h)
end
end</langsyntaxhighlight>
 
{{out}}
Line 2,280 ⟶ 2,381:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.security.MessageDigest
Line 2,291 ⟶ 2,392:
for (byte in digest) print("%02x".format(byte))
println()
}</langsyntaxhighlight>
 
{{out}}
Line 2,299 ⟶ 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 2,314 ⟶ 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 2,324 ⟶ 2,425:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">'[RC]MD5
'from tsh73's January 2008 code
 
Line 2,468 ⟶ 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 2,474 ⟶ 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 2,752 ⟶ 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 2,786 ⟶ 2,887:
end repeat
put results
end md5testsuite</langsyntaxhighlight>
Output
<pre>ok abc
Line 2,798 ⟶ 2,899:
=={{header|Lua}}==
Using the [http://www.keplerproject.org/md5/ Kepler MD5 library]:
<langsyntaxhighlight Lualang="lua">require "md5"
 
--printing a sum:
Line 2,813 ⟶ 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 2,839 ⟶ 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 2,966 ⟶ 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,994 ⟶ 3,095:
 
/* Output in lowercase hex */
$print(base_encode(result, "0123456789abcdef"));</langsyntaxhighlight>
 
{{out}}
Line 3,003 ⟶ 3,104:
=={{header|Nemerle}}==
{{trans|C#}}
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
using System.Text;
Line 3,036 ⟶ 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 3,100 ⟶ 3,201:
return digestSum
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 3,138 ⟶ 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 3,153 ⟶ 3,254:
=={{header|Oberon-2}}==
{{works with|oo2c}}{{libheader|crypto}}
<langsyntaxhighlight lang="oberon2">
MODULE MD5;
IMPORT
Line 3,171 ⟶ 3,272:
Out.String("MD5: ");Utils.PrintHex(str,0,h.size);Out.Ln
END MD5.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,179 ⟶ 3,280:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class MD5 {
function : Main(args : String[]) ~ Nil {
Line 3,187 ⟶ 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 3,208 ⟶ 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 3,222 ⟶ 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 3,260 ⟶ 3,361:
(test "abcdefghijklmnopqrstuvwxyz")
(test "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
(test "12345678901234567890123456789012345678901234567890123456789012345678901234567890")</langsyntaxhighlight>
 
<pre>
Line 3,274 ⟶ 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 3,282 ⟶ 3,383:
6 STRING( HEX-ENCODE( MD5-DIGEST( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ) ) ) SKIP
7 STRING( HEX-ENCODE( MD5-DIGEST( "12345678901234567890123456789012345678901234567890123456789012345678901234567890" ) ) )
VIEW-AS ALERT-BOX</langsyntaxhighlight>
 
Output:
Line 3,304 ⟶ 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 3,330 ⟶ 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 3,343 ⟶ 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 3,365 ⟶ 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 3,378 ⟶ 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 3,390 ⟶ 3,491:
writeln(strEncrypted);
end.
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,400 ⟶ 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}}==
Line 3,416 ⟶ 3,517:
 
=={{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 3,452 ⟶ 3,553:
; Call StringFingerprint() function and display MD5 result in Debug window
Debug StringFingerprint(test$, #PB_Cipher_MD5)
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
Line 3,458 ⟶ 3,559:
 
;Python 3.x, 2.5 and later 2.x versions:
<langsyntaxhighlight lang="python">>>> import hashlib
>>> # RFC 1321 test suite:
>>> tests = (
Line 3,470 ⟶ 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 3,499 ⟶ 3,600:
(md5 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
(md5 "12345678901234567890123456789012345678901234567890123456789012345678901234567890")
</syntaxhighlight>
</lang>
 
Output:
Line 3,516 ⟶ 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 3,637 ⟶ 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 3,663 ⟶ 3,764:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
See MD5("my string!") + nl
# output : a83a049fbe50cf7334caa86bf16a3520
</syntaxhighlight>
</lang>
 
=={{header|RLaB}}==
Line 3,675 ⟶ 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 3,771 ⟶ 3,872:
endfor;
return;
end-proc;</langsyntaxhighlight>
Note that this implementation converts the input from EBCDIC to ASCII before computing the hash.
 
Line 3,782 ⟶ 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 3,805 ⟶ 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 3,817 ⟶ 3,918:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object RosettaMD5 extends App {
 
def MD5(s: String): String = {
Line 3,837 ⟶ 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 3,846 ⟶ 3,947:
begin
writeln(hex(md5("The quick brown fox jumped over the lazy dog's back")));
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,855 ⟶ 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 3,871 ⟶ 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 3,894 ⟶ 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 3,930 ⟶ 4,037:
 
End Module
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,937 ⟶ 4,044:
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import crypto.md5
fn main() {
Line 3,970 ⟶ 4,077:
println('MD5 succeeded $s')
}
}</langsyntaxhighlight>
{{out}}
<pre>MD5 succeeded
Line 3,986 ⟶ 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 3,998 ⟶ 4,105:
var digest = Md5.digest(s)
Fmt.print("$s <== '$0s'", digest, s)
}</langsyntaxhighlight>
 
{{out}}
Line 4,009 ⟶ 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,060

edits