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.

      PRINT FN_MD5("")
      PRINT FN_MD5("a")
      PRINT FN_MD5("abc")
      PRINT FN_MD5("message digest")
      PRINT FN_MD5("abcdefghijklmnopqrstuvwxyz")
      PRINT FN_MD5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
      PRINT FN_MD5(STRING$(8,"1234567890"))
      END
 
      DEF FN_MD5(message$)
      LOCAL a%, b%, c%, d%, f%, g%, h0%, h1%, h2%, h3%, i%, bits%, chunk%, temp%
      LOCAL r&(), k%(), w%()
      DIM r&(63), k%(63), w%(15)
 
      REM r specifies the per-round shift amounts:
      r&() = 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
 
      REM Use binary integer part of the sines of integers (Radians) as constants:
      FOR i% = 0 TO 63
        k%(i%) = FN32(INT(ABS(SIN(i% + 1.0#)) * 2^32))
      NEXT
 
      REM Initialize variables:
      h0% = &67452301
      h1% = &EFCDAB89
      h2% = &98BADCFE
      h3% = &10325476
 
      bits% = LEN(message$)*8
 
      REM Append '1' bit to message:
      message$ += CHR$&80
 
      REM Append '0' bits until message length in bits = 448 (mod 512):
      WHILE (LEN(message$) MOD 64) <> 56
        message$ += CHR$0
      ENDWHILE
 
      REM Append length of message (before pre-processing), in bits, as
      REM 64-bit little-endian integer:
      FOR i% = 0 TO 56 STEP 8
        message$ += CHR$(bits% >>> i%)
      NEXT
 
      REM Process the message in successive 512-bit chunks:
      FOR chunk% = 0 TO LEN(message$) DIV 64 - 1
 
        REM Break chunk into sixteen 32-bit little-endian words:
        FOR i% = 0 TO 15
          w%(i%) = !(PTR(message$) + 64*chunk% + 4*i%)
        NEXT i%
 
        REM Initialize hash value for this chunk:
        a% = h0%
        b% = h1%
        c% = h2%
        d% = h3%
 
        REM Main loop:
        FOR i% = 0 TO 63
          CASE TRUE OF
            WHEN i% <= 15:
              f% = d% EOR (b% AND (c% EOR d%))
              g% = i%
            WHEN 16 <= i% AND i% <= 31:
              f% = c% EOR (d% AND (b% EOR c%))
              g% = (5 * i% + 1) MOD 16
            WHEN 32 <= i% AND i% <= 47:
              f% = b% EOR c% EOR d%
              g% = (3 * i% + 5) MOD 16
            OTHERWISE:
              f% = c% EOR (b% OR (NOT d%))
              g% = (7 * i%) MOD 16
          ENDCASE
 
          temp% = d%
          d% = c%
          c% = b%
          b% = FN32(b% + FNlrot(FN32(a% + f%) + FN32(k%(i%) + w%(g%)), r&(i%)))
          a% = temp%
 
        NEXT i%
 
        REM Add this chunk's hash to result so far:
        h0% = FN32(h0% + a%)
        h1% = FN32(h1% + b%)
        h2% = FN32(h2% + c%)
        h3% = FN32(h3% + d%)
 
      NEXT chunk%
 
      = FNrevhex(h0%) + FNrevhex(h1%) + FNrevhex(h2%) + FNrevhex(h3%)
 
      DEF FNrevhex(A%)
      SWAP ?(^A%+0),?(^A%+3)
      SWAP ?(^A%+1),?(^A%+2)
      = RIGHT$("0000000"+STR$~A%,8)
 
      DEF FNlrot(n#, r%)
      n# = FN32(n#)
      = (n# << r%) OR (n# >>> (32 - r%))
 
      DEF FN32(n#)
      WHILE n# > &7FFFFFFF : n# -= 2^32 : ENDWHILE
      WHILE n# < &80000000 : n# += 2^32 : ENDWHILE
      = n#