The purpose of this task to code and validate an implementation of the MD5 Message Digest Algorithm by coding the algorithm directly (not using a call to a built-in or external hashing library). For details of the algorithm refer to MD5 on Wikipedia or the MD5 definition in IETF RFC (1321).

 The implementation needs to implement the key functionality namely producing a correct message digest for an input string. It is not necessary to mimic all of the calling modes such as adding to a digest one block at a time over subsequent calls. 
 In addition to coding and verifying your implementation, note any challenges your language presented implementing the solution, implementation choices made, or limitations of your solution. 
 Solutions on this page should implement MD5 directly and NOT use built in (MD5) functions, call outs to operating system calls or library routines written in other languages as is common in the original MD5 task.
 The following are acceptable:
 An original implementation from the specification, reference implementation, or pseudo-code
 A translation of a correct implementation from another language
 A library routine in the same language; however, the source must be included here.


The solutions shown here will provide practical illustrations of bit manipulation, unsigned integers, working with little-endian data. Additionally, the task requires an attention to details such as boundary conditions since being out by even 1 bit will produce dramatically different results. Subtle implementation bugs can result in some hashes being correct while others are wrong. Not only is it critical to get the individual sub functions working correctly, even small errors in padding, endianness, or data layout will result in failure.

RFC 1321
                            hash code <== string 
   0xd41d8cd98f00b204e9800998ecf8427e <== ""  
   0x0cc175b9c0f1b6a831c399e269772661 <== "a"
   0x900150983cd24fb0d6963f7d28e17f72 <== "abc"
   0xf96b697d7cb7938d525a2f31aaf161d0 <== "message digest"
   0xc3fcd3d76192e4007dfb496cca67e13b <== "abcdefghijklmnopqrstuvwxyz"
   0xd174ab98d277d9f5a5611c2c9f419d9f <== "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
   0x57edf4a22be3c955ac49da2e2107b67a <== "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
In addition, intermediate outputs to aid in developing an implementation can be found here.

The MD5 Message-Digest Algorithm was developed by RSA Data Security, Inc. in 1991.



 Warning
Rosetta Code is not a place you should rely on for examples of code in critical roles, including security.
Also, note that MD5 has been broken and should not be used in applications requiring security. For these consider SHA2 or the upcoming SHA3.

----------------------------------------
-- Calculates MD5 hash of string or bytearray
-- @param {bytearray|string} input
-- @return {bytearray} (16 bytes)
----------------------------------------
on md5 (input)
    if stringP(input) then input = bytearray(input)
 
    -- Convert string to list of little-endian words...
    t_iLen = input.length * 8
    t_iCnt = (t_iLen + 64) / 512 * 16 + 16
 
    -- Create list, fill with zeros...
    x = []
    x[t_iCnt] = 0
 
    t_fArr = [1, 256, 65536, 16777216]
    i = 0
    j = 0
    repeat while i < t_iLen
        j = j + 1
        t_iNext = i / 32 + 1
        t_iTemp = bitAnd(input[i/8+1], 255) * t_fArr[j]
        x[t_iNext] = bitOr(x[t_iNext], t_iTemp)
        i = i + 8
        j = j mod 4
    end repeat
 
    -- Append padding...
    t_iNext = t_iLen / 32 + 1
    x[t_iNext] = bitOr(x[t_iNext], 128 * t_fArr[j + 1])
    x[(t_iLen + 64) / 512 * 16 + 15] = t_iLen
 
    -- Actual algorithm starts here...
    a = 1732584193
    b = -271733879
    c = -1732584194
    d = 271733878
    i = 1
    t_iWrap = the maxInteger + 1
    t_iCount = x.count + 1
    repeat while i < t_iCount
        olda = a
        oldb = b
        oldc = c
        oldd = d
 
        -- Round(1) --
        n = bitOr(bitAnd(b, c), bitAnd(bitNot(b), d)) + a + x[i] - 680876936
        if(n < 0) then a = bitOr(n * 128, bitOr((n + t_iWrap) / 33554432, 64)) + b
        else a = bitOr(n * 128, n / 33554432) + b
        n = bitOr(bitAnd(a, b), bitAnd(bitNot(a), c)) + d + x[i + 1] - 389564586
        if(n < 0) then d = bitOr(n * 4096, bitOr((n + t_iWrap) / 1048576, 2048)) + a
        else d = bitOr(n * 4096, n / 1048576) + a
        n = bitOr(bitAnd(d, a), bitAnd(bitNot(d), b)) + c + x[i + 2] + 606105819
        if(n < 0) then c = bitOr(n * 131072, bitOr((n + t_iWrap) / 32768, 65536)) + d
        else c = bitOr(n * 131072, n / 32768) + d
        n = bitOr(bitAnd(c, d), bitAnd(bitNot(c), a)) + b + x[i + 3] - 1044525330
        if(n < 0) then b = bitOr(n * 4194304, bitOr((n + t_iWrap) / 1024, 2097152)) + c
        else b = bitOr(n * 4194304, n / 1024) + c
        n = bitOr(bitAnd(b, c), bitAnd(bitNot(b), d)) + a + x[i + 4] - 176418897
        if(n < 0) then a = bitOr(n * 128, bitOr((n + t_iWrap) / 33554432, 64)) + b
        else a = bitOr(n * 128, n / 33554432) + b
        n = bitOr(bitAnd(a, b), bitAnd(bitNot(a), c)) + d + x[i + 5] + 1200080426
        if(n < 0) then d = bitOr(n * 4096, bitOr((n + t_iWrap) / 1048576, 2048)) + a
        else d = bitOr(n * 4096, n / 1048576) + a
        n = bitOr(bitAnd(d, a), bitAnd(bitNot(d), b)) + c + x[i + 6] - 1473231341
        if(n < 0) then c = bitOr(n * 131072, bitOr((n + t_iWrap) / 32768, 65536)) + d
        else c = bitOr(n * 131072, n / 32768) + d
        n = bitOr(bitAnd(c, d), bitAnd(bitNot(c), a)) + b + x[i + 7] - 45705983
        if(n < 0) then b = bitOr(n * 4194304, bitOr((n + t_iWrap) / 1024, 2097152)) + c
        else b = bitOr(n * 4194304, n / 1024) + c
        n = bitOr(bitAnd(b, c), bitAnd(bitNot(b), d)) + a + x[i + 8] + 1770035416
        if(n < 0) then a = bitOr(n * 128, bitOr((n + t_iWrap) / 33554432, 64)) + b
        else a = bitOr(n * 128, n / 33554432) + b
        n = bitOr(bitAnd(a, b), bitAnd(bitNot(a), c)) + d + x[i + 9] - 1958414417
        if(n < 0) then d = bitOr(n * 4096, bitOr((n + t_iWrap) / 1048576, 2048)) + a
        else d = bitOr(n * 4096, n / 1048576) + a
        n = bitOr(bitAnd(d, a), bitAnd(bitNot(d), b)) + c + x[i + 10] - 42063
        if(n < 0) then c = bitOr(n * 131072, bitOr((n + t_iWrap) / 32768, 65536)) + d
        else c = bitOr(n * 131072, n / 32768) + d
        n = bitOr(bitAnd(c, d), bitAnd(bitNot(c), a)) + b + x[i + 11] - 1990404162
        if(n < 0) then b = bitOr(n * 4194304, bitOr((n + t_iWrap) / 1024, 2097152)) + c
        else b = bitOr(n * 4194304, n / 1024) + c
        n = bitOr(bitAnd(b, c), bitAnd(bitNot(b), d)) + a + x[i + 12] + 1804603682
        if(n < 0) then a = bitOr(n * 128, bitOr((n + t_iWrap) / 33554432, 64)) + b
        else a = bitOr(n * 128, n / 33554432) + b
        n = bitOr(bitAnd(a, b), bitAnd(bitNot(a), c)) + d + x[i + 13] - 40341101
        if(n < 0) then d = bitOr(n * 4096, bitOr((n + t_iWrap) / 1048576, 2048)) + a
        else d = bitOr(n * 4096, n / 1048576) + a
        n = bitOr(bitAnd(d, a), bitAnd(bitNot(d), b)) + c + x[i + 14] - 1502002290
        if(n < 0) then c = bitOr(n * 131072, bitOr((n + t_iWrap) / 32768, 65536)) + d
        else c = bitOr(n * 131072, n / 32768) + d
        n = bitOr(bitAnd(c, d), bitAnd(bitNot(c), a)) + b + x[i + 15] + 1236535329
        if(n < 0) then b = bitOr(n * 4194304, bitOr((n + t_iWrap) / 1024, 2097152)) + c
        else b = bitOr(n * 4194304, n / 1024) + c
 
        -- Round(2) --
        n = bitOr(bitAnd(b, d), bitAnd(c, bitNot(d))) + a + x[i + 1] - 165796510
        if(n < 0) then a = bitOr(n * 32, bitOr((n + t_iWrap) / 134217728, 16)) + b
        else a = bitOr(n * 32, n / 134217728) + b
        n = bitOr(bitAnd(a, c), bitAnd(b, bitNot(c))) + d + x[i + 6] - 1069501632
        if(n < 0) then d = bitOr(n * 512, bitOr((n + t_iWrap) / 8388608, 256)) + a
        else d = bitOr(n * 512, n / 8388608) + a
        n = bitOr(bitAnd(d, b), bitAnd(a, bitNot(b))) + c + x[i + 11] + 643717713
        if(n < 0) then c = bitOr(n * 16384, bitOr((n + t_iWrap) / 262144, 8192)) + d
        else c = bitOr(n * 16384, n / 262144) + d
        n = bitOr(bitAnd(c, a), bitAnd(d, bitNot(a))) + b + x[i] - 373897302
        if(n < 0) then b = bitOr(n * 1048576, bitOr((n + t_iWrap) / 4096, 524288)) + c
        else b = bitOr(n * 1048576, n / 4096) + c
        n = bitOr(bitAnd(b, d), bitAnd(c, bitNot(d))) + a + x[i + 5] - 701558691
        if(n < 0) then a = bitOr(n * 32, bitOr((n + t_iWrap) / 134217728, 16)) + b
        else a = bitOr(n * 32, n / 134217728) + b
        n = bitOr(bitAnd(a, c), bitAnd(b, bitNot(c))) + d + x[i + 10] + 38016083
        if(n < 0) then d = bitOr(n * 512, bitOr((n + t_iWrap) / 8388608, 256)) + a
        else d = bitOr(n * 512, n / 8388608) + a
        n = bitOr(bitAnd(d, b), bitAnd(a, bitNot(b))) + c + x[i + 15] - 660478335
        if(n < 0) then c = bitOr(n * 16384, bitOr((n + t_iWrap) / 262144, 8192)) + d
        else c = bitOr(n * 16384, n / 262144) + d
        n = bitOr(bitAnd(c, a), bitAnd(d, bitNot(a))) + b + x[i + 4] - 405537848
        if(n < 0) then b = bitOr(n * 1048576, bitOr((n + t_iWrap) / 4096, 524288)) + c
        else b = bitOr(n * 1048576, n / 4096) + c
        n = bitOr(bitAnd(b, d), bitAnd(c, bitNot(d))) + a + x[i + 9] + 568446438
        if(n < 0) then a = bitOr(n * 32, bitOr((n + t_iWrap) / 134217728, 16)) + b
        else a = bitOr(n * 32, n / 134217728) + b
        n = bitOr(bitAnd(a, c), bitAnd(b, bitNot(c))) + d + x[i + 14] - 1019803690
        if(n < 0) then d = bitOr(n * 512, bitOr((n + t_iWrap) / 8388608, 256)) + a
        else d = bitOr(n * 512, n / 8388608) + a
        n = bitOr(bitAnd(d, b), bitAnd(a, bitNot(b))) + c + x[i + 3] - 187363961
        if(n < 0) then c = bitOr(n * 16384, bitOr((n + t_iWrap) / 262144, 8192)) + d
        else c = bitOr(n * 16384, n / 262144) + d
        n = bitOr(bitAnd(c, a), bitAnd(d, bitNot(a))) + b + x[i + 8] + 1163531501
        if(n < 0) then b = bitOr(n * 1048576, bitOr((n + t_iWrap) / 4096, 524288)) + c
        else b = bitOr(n * 1048576, n / 4096) + c
        n = bitOr(bitAnd(b, d), bitAnd(c, bitNot(d))) + a + x[i + 13] - 1444681467
        if(n < 0) then a = bitOr(n * 32, bitOr((n + t_iWrap) / 134217728, 16)) + b
        else a = bitOr(n * 32, n / 134217728) + b
        n = bitOr(bitAnd(a, c), bitAnd(b, bitNot(c))) + d + x[i + 2] - 51403784
        if(n < 0) then d = bitOr(n * 512, bitOr((n + t_iWrap) / 8388608, 256)) + a
        else d = bitOr(n * 512, n / 8388608) + a
        n = bitOr(bitAnd(d, b), bitAnd(a, bitNot(b))) + c + x[i + 7] + 1735328473
        if(n < 0) then c = bitOr(n * 16384, bitOr((n + t_iWrap) / 262144, 8192)) + d
        else c = bitOr(n * 16384, n / 262144) + d
        n = bitOr(bitAnd(c, a), bitAnd(d, bitNot(a))) + b + x[i + 12] - 1926607734
        if(n < 0) then b = bitOr(n * 1048576, bitOr((n + t_iWrap) / 4096, 524288)) + c
        else b = bitOr(n * 1048576, n / 4096) + c
 
        -- Round(3) --
        n = bitXor(bitXor(b, c), d) + a + x[i + 5] - 378558
        if(n < 0) then a = bitOr(n * 16, bitOr((n + t_iWrap) / 268435456, 8)) + b
        else a = bitOr(n * 16, n / 268435456) + b
        n = bitXor(bitXor(a, b), c) + d + x[i + 8] - 2022574463
        if(n < 0) then d = bitOr(n * 2048, bitOr((n + t_iWrap) / 2097152, 1024)) + a
        else d = bitOr(n * 2048, n / 2097152) + a
        n = bitXor(bitXor(d, a), b) + c + x[i + 11] + 1839030562
        if(n < 0) then c = bitOr(n * 65536, bitOr((n + t_iWrap) / 65536, 32768)) + d
        else c = bitOr(n * 65536, n / 65536) + d
        n = bitXor(bitXor(c, d), a) + b + x[i + 14] - 35309556
        if(n < 0) then b = bitOr(n * 8388608, bitOr((n + t_iWrap) / 512, 4194304)) + c
        else b = bitOr(n * 8388608, n / 512) + c
        n = bitXor(bitXor(b, c), d) + a + x[i + 1] - 1530992060
        if(n < 0) then a = bitOr(n * 16, bitOr((n + t_iWrap) / 268435456, 8)) + b
        else a = bitOr(n * 16, n / 268435456) + b
        n = bitXor(bitXor(a, b), c) + d + x[i + 4] + 1272893353
        if(n < 0) then d = bitOr(n * 2048, bitOr((n + t_iWrap) / 2097152, 1024)) + a
        else d = bitOr(n * 2048, n / 2097152) + a
        n = bitXor(bitXor(d, a), b) + c + x[i + 7] - 155497632
        if(n < 0) then c = bitOr(n * 65536, bitOr((n + t_iWrap) / 65536, 32768)) + d
        else c = bitOr(n * 65536, n / 65536) + d
        n = bitXor(bitXor(c, d), a) + b + x[i + 10] - 1094730640
        if(n < 0) then b = bitOr(n * 8388608, bitOr((n + t_iWrap) / 512, 4194304)) + c
        else b = bitOr(n * 8388608, n / 512) + c
        n = bitXor(bitXor(b, c), d) + a + x[i + 13] + 681279174
        if(n < 0) then a = bitOr(n * 16, bitOr((n + t_iWrap) / 268435456, 8)) + b
        else a = bitOr(n * 16, n / 268435456) + b
        n = bitXor(bitXor(a, b), c) + d + x[i] - 358537222
        if(n < 0) then d = bitOr(n * 2048, bitOr((n + t_iWrap) / 2097152, 1024)) + a
        else d = bitOr(n * 2048, n / 2097152) + a
        n = bitXor(bitXor(d, a), b) + c + x[i + 3] - 722521979
        if(n < 0) then c = bitOr(n * 65536, bitOr((n + t_iWrap) / 65536, 32768)) + d
        else c = bitOr(n * 65536, n / 65536) + d
        n = bitXor(bitXor(c, d), a) + b + x[i + 6] + 76029189
        if(n < 0) then b = bitOr(n * 8388608, bitOr((n + t_iWrap) / 512, 4194304)) + c
        else b = bitOr(n * 8388608, n / 512) + c
        n = bitXor(bitXor(b, c), d) + a + x[i + 9] - 640364487
        if(n < 0) then a = bitOr(n * 16, bitOr((n + t_iWrap) / 268435456, 8)) + b
        else a = bitOr(n * 16, n / 268435456) + b
        n = bitXor(bitXor(a, b), c) + d + x[i + 12] - 421815835
        if(n < 0) then d = bitOr(n * 2048, bitOr((n + t_iWrap) / 2097152, 1024)) + a
        else d = bitOr(n * 2048, n / 2097152) + a
        n = bitXor(bitXor(d, a), b) + c + x[i + 15] + 530742520
        if(n < 0) then c = bitOr(n * 65536, bitOr((n + t_iWrap) / 65536, 32768)) + d
        else c = bitOr(n * 65536, n / 65536) + d
        n = bitXor(bitXor(c, d), a) + b + x[i + 2] - 995338651
        if(n < 0) then b = bitOr(n * 8388608, bitOr((n + t_iWrap) / 512, 4194304)) + c
        else b = bitOr(n * 8388608, n / 512) + c
 
        -- Round(4) --
        n = bitXor(c, bitOr(b, bitNot(d))) + a + x[i] - 198630844
        if(n < 0) then a = bitOr(n * 64, bitOr((n + t_iWrap) / 67108864, 32)) + b
        else a = bitOr(n * 64, n / 67108864) + b
        n = bitXor(b, bitOr(a, bitNot(c))) + d + x[i + 7] + 1126891415
        if(n < 0) then d = bitOr(n * 1024, bitOr((n + t_iWrap) / 4194304, 512)) + a
        else d = bitOr(n * 1024, n / 4194304) + a
        n = bitXor(a, bitOr(d, bitNot(b))) + c + x[i + 14] - 1416354905
        if(n < 0) then c = bitOr(n * 32768, bitOr((n + t_iWrap) / 131072, 16384)) + d
        else c = bitOr(n * 32768, n / 131072) + d
        n = bitXor(d, bitOr(c, bitNot(a))) + b + x[i + 5] - 57434055
        if(n < 0) then b = bitOr(n * 2097152, bitOr((n + t_iWrap) / 2048, 1048576)) + c
        else b = bitOr(n * 2097152, n / 2048) + c
        n = bitXor(c, bitOr(b, bitNot(d))) + a + x[i + 12] + 1700485571
        if(n < 0) then a = bitOr(n * 64, bitOr((n + t_iWrap) / 67108864, 32)) + b
        else a = bitOr(n * 64, n / 67108864) + b
        n = bitXor(b, bitOr(a, bitNot(c))) + d + x[i + 3] - 1894986606
        if(n < 0) then d = bitOr(n * 1024, bitOr((n + t_iWrap) / 4194304, 512)) + a
        else d = bitOr(n * 1024, n / 4194304) + a
        n = bitXor(a, bitOr(d, bitNot(b))) + c + x[i + 10] - 1051523
        if(n < 0) then c = bitOr(n * 32768, bitOr((n + t_iWrap) / 131072, 16384)) + d
        else c = bitOr(n * 32768, n / 131072) + d
        n = bitXor(d, bitOr(c, bitNot(a))) + b + x[i + 1] - 2054922799
        if(n < 0) then b = bitOr(n * 2097152, bitOr((n + t_iWrap) / 2048, 1048576)) + c
        else b = bitOr(n * 2097152, n / 2048) + c
        n = bitXor(c, bitOr(b, bitNot(d))) + a + x[i + 8] + 1873313359
        if(n < 0) then a = bitOr(n * 64, bitOr((n + t_iWrap) / 67108864, 32)) + b
        else a = bitOr(n * 64, n / 67108864) + b
        n = bitXor(b, bitOr(a, bitNot(c))) + d + x[i + 15] - 30611744
        if(n < 0) then d = bitOr(n * 1024, bitOr((n + t_iWrap) / 4194304, 512)) + a
        else d = bitOr(n * 1024, n / 4194304) + a
        n = bitXor(a, bitOr(d, bitNot(b))) + c + x[i + 6] - 1560198380
        if(n < 0) then c = bitOr(n * 32768, bitOr((n + t_iWrap) / 131072, 16384)) + d
        else c = bitOr(n * 32768, n / 131072) + d
        n = bitXor(d, bitOr(c, bitNot(a))) + b + x[i + 13] + 1309151649
        if(n < 0) then b = bitOr(n * 2097152, bitOr((n + t_iWrap) / 2048, 1048576)) + c
        else b = bitOr(n * 2097152, n / 2048) + c
        n = bitXor(c, bitOr(b, bitNot(d))) + a + x[i + 4] - 145523070
        if(n < 0) then a = bitOr(n * 64, bitOr((n + t_iWrap) / 67108864, 32)) + b
        else a = bitOr(n * 64, n / 67108864) + b
        n = bitXor(b, bitOr(a, bitNot(c))) + d + x[i + 11] - 1120210379
        if(n < 0) then d = bitOr(n * 1024, bitOr((n + t_iWrap) / 4194304, 512)) + a
        else d = bitOr(n * 1024, n / 4194304) + a
        n = bitXor(a, bitOr(d, bitNot(b))) + c + x[i + 2] + 718787259
        if(n < 0) then c = bitOr(n * 32768, bitOr((n + t_iWrap) / 131072, 16384)) + d
        else c = bitOr(n * 32768, n / 131072) + d
        n = bitXor(d, bitOr(c, bitNot(a))) + b + x[i + 9] - 343485551
        if(n < 0) then b = bitOr(n * 2097152, bitOr((n + t_iWrap) / 2048, 1048576)) + c
        else b = bitOr(n * 2097152, n / 2048) + c
        a = a + olda
        b = b + oldb
        c = c + oldc
        d = d + oldd
        i = i + 16
    end repeat
 
    t_iArr = [a, b, c, d]
    ba = bytearray()
    p = 1
    repeat with i in t_iArr
        if(i > 0) then
            repeat with n = 1 to 4
                ba[p] = (i mod 256)
                i = i / 256
                p = p+1
            end repeat
        else
            i = bitNot(i)
            repeat with n = 1 to 4
                ba[p] = 255-(i mod 256)
                i = i / 256
                p = p+1
            end repeat
        end if
    end repeat
    ba.position = 1
    return ba
end