md5 · JavaScript Function to Calculate MD5 of a String

Back to the Live Example

Description

Below is a JavaScript function to calculate MD5 of a string. The string can be either ASCII or Unicode. Unicode strings will be encoded in UTF-8.
function md5 (input) {

    function addUnsigned (lx, ly) {
        var lx4, ly4, lx8, ly8, lresult
        lx8 = lx & 0x80000000
        ly8 = ly & 0x80000000
        lx4 = lx & 0x40000000
        ly4 = ly & 0x40000000
        lresult = (lx & 0x3fffffff) + (ly & 0x3fffffff)
        if (lx4 & ly4) {
            return lresult ^ 0x80000000 ^ lx8 ^ ly8
        }
        if (lx4 | ly4) {
            if (lresult & 0x40000000) {
                return lresult ^ 0xc0000000 ^ lx8 ^ ly8
            }
            return lresult ^ 0x40000000 ^ lx8 ^ ly8
        }
        return lresult ^ lx8 ^ ly8
    }

    function _f (x, y, z) {
        return (x & y) | ((~x) & z)
    }

    function _g (x, y, z) {
        return (x & z) | (y & (~z))
    }

    function _h (x, y, z) {
        return x ^ y ^ z
    }

    function _i (x, y, z) {
        return y ^ (x | (~z))
    }

    function ff (a, b, c, d, x, s, ac) {
        a = addUnsigned(a, addUnsigned(addUnsigned(_f(b, c, d), x), ac))
        return addUnsigned(rotateLeft(a, s), b)
    }

    function gg (a, b, c, d, x, s, ac) {
        a = addUnsigned(a, addUnsigned(addUnsigned(_g(b, c, d), x), ac))
        return addUnsigned(rotateLeft(a, s), b)
    }

    function hh (a, b, c, d, x, s, ac) {
        a = addUnsigned(a, addUnsigned(addUnsigned(_h(b, c, d), x), ac))
        return addUnsigned(rotateLeft(a, s), b)
    }

    function ii (a, b, c, d, x, s, ac) {
        a = addUnsigned(a, addUnsigned(addUnsigned(_i(b, c, d), x), ac))
        return addUnsigned(rotateLeft(a, s), b)
    }

    function rotateLeft (value, bits) {
        return (value << bits) | (value >>> (32 - bits))
    }

    function toWords (input) {
        var length = input.length
        var numForwards = (((length + 8 - ((length + 8) % 64)) / 64) + 1) * 16
        var index, position = 0, words = new Array(numForwards - 1)
        for (var i = 0; i < length; i++) {
            index = (i - (i % 4)) / 4
            position = (i % 4) * 8
            words[index] = words[index] | (input.charCodeAt(i) << position)
        }
        index = (i - (i % 4)) / 4
        position = (i % 4) * 8
        words[index] = words[index] | (0x80 << position)
        words[numForwards - 2] = length << 3
        words[numForwards - 1] = length >>> 29
        return words
    }

    function hex (n) {
        n = (n >>> 0).toString(16)
        while (n.length < 8) n = '0' + n
        n = n.match(/../g)
        return [n[3], n[2], n[1], n[0]].join('')
    }

    input = unescape(encodeURIComponent(input))
    var w = toWords(input)
    var a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476

    var s11 = 7, s12 = 12, s13 = 17, s14 = 22,
        s21 = 5, s22 = 9, s23 = 14, s24 = 20,
        s31 = 4, s32 = 11, s33 = 16, s34 = 23,
        s41 = 6, s42 = 10, s43 = 15, s44 = 21

    for (var i = 0; i < w.length; i += 16) {
        var aa = a, bb = b, cc = c, dd = d
        a = ff(a, b, c, d, w[i +  0], s11, 0xd76aa478)
        d = ff(d, a, b, c, w[i +  1], s12, 0xe8c7b756)
        c = ff(c, d, a, b, w[i +  2], s13, 0x242070db)
        b = ff(b, c, d, a, w[i +  3], s14, 0xc1bdceee)
        a = ff(a, b, c, d, w[i +  4], s11, 0xf57c0faf)
        d = ff(d, a, b, c, w[i +  5], s12, 0x4787c62a)
        c = ff(c, d, a, b, w[i +  6], s13, 0xa8304613)
        b = ff(b, c, d, a, w[i +  7], s14, 0xfd469501)
        a = ff(a, b, c, d, w[i +  8], s11, 0x698098d8)
        d = ff(d, a, b, c, w[i +  9], s12, 0x8b44f7af)
        c = ff(c, d, a, b, w[i + 10], s13, 0xffff5bb1)
        b = ff(b, c, d, a, w[i + 11], s14, 0x895cd7be)
        a = ff(a, b, c, d, w[i + 12], s11, 0x6b901122)
        d = ff(d, a, b, c, w[i + 13], s12, 0xfd987193)
        c = ff(c, d, a, b, w[i + 14], s13, 0xa679438e)
        b = ff(b, c, d, a, w[i + 15], s14, 0x49b40821)
        a = gg(a, b, c, d, w[i +  1], s21, 0xf61e2562)
        d = gg(d, a, b, c, w[i +  6], s22, 0xc040b340)
        c = gg(c, d, a, b, w[i + 11], s23, 0x265e5a51)
        b = gg(b, c, d, a, w[i +  0], s24, 0xe9b6c7aa)
        a = gg(a, b, c, d, w[i +  5], s21, 0xd62f105d)
        d = gg(d, a, b, c, w[i + 10], s22, 0x02441453)
        c = gg(c, d, a, b, w[i + 15], s23, 0xd8a1e681)
        b = gg(b, c, d, a, w[i +  4], s24, 0xe7d3fbc8)
        a = gg(a, b, c, d, w[i +  9], s21, 0x21e1cde6)
        d = gg(d, a, b, c, w[i + 14], s22, 0xc33707d6)
        c = gg(c, d, a, b, w[i +  3], s23, 0xf4d50d87)
        b = gg(b, c, d, a, w[i +  8], s24, 0x455a14ed)
        a = gg(a, b, c, d, w[i + 13], s21, 0xa9e3e905)
        d = gg(d, a, b, c, w[i +  2], s22, 0xfcefa3f8)
        c = gg(c, d, a, b, w[i +  7], s23, 0x676f02d9)
        b = gg(b, c, d, a, w[i + 12], s24, 0x8d2a4c8a)
        a = hh(a, b, c, d, w[i +  5], s31, 0xfffa3942)
        d = hh(d, a, b, c, w[i +  8], s32, 0x8771f681)
        c = hh(c, d, a, b, w[i + 11], s33, 0x6d9d6122)
        b = hh(b, c, d, a, w[i + 14], s34, 0xfde5380c)
        a = hh(a, b, c, d, w[i +  1], s31, 0xa4beea44)
        d = hh(d, a, b, c, w[i +  4], s32, 0x4bdecfa9)
        c = hh(c, d, a, b, w[i +  7], s33, 0xf6bb4b60)
        b = hh(b, c, d, a, w[i + 10], s34, 0xbebfbc70)
        a = hh(a, b, c, d, w[i + 13], s31, 0x289b7ec6)
        d = hh(d, a, b, c, w[i +  0], s32, 0xeaa127fa)
        c = hh(c, d, a, b, w[i +  3], s33, 0xd4ef3085)
        b = hh(b, c, d, a, w[i +  6], s34, 0x04881d05)
        a = hh(a, b, c, d, w[i +  9], s31, 0xd9d4d039)
        d = hh(d, a, b, c, w[i + 12], s32, 0xe6db99e5)
        c = hh(c, d, a, b, w[i + 15], s33, 0x1fa27cf8)
        b = hh(b, c, d, a, w[i +  2], s34, 0xc4ac5665)
        a = ii(a, b, c, d, w[i +  0], s41, 0xf4292244)
        d = ii(d, a, b, c, w[i +  7], s42, 0x432aff97)
        c = ii(c, d, a, b, w[i + 14], s43, 0xab9423a7)
        b = ii(b, c, d, a, w[i +  5], s44, 0xfc93a039)
        a = ii(a, b, c, d, w[i + 12], s41, 0x655b59c3)
        d = ii(d, a, b, c, w[i +  3], s42, 0x8f0ccc92)
        c = ii(c, d, a, b, w[i + 10], s43, 0xffeff47d)
        b = ii(b, c, d, a, w[i +  1], s44, 0x85845dd1)
        a = ii(a, b, c, d, w[i +  8], s41, 0x6fa87e4f)
        d = ii(d, a, b, c, w[i + 15], s42, 0xfe2ce6e0)
        c = ii(c, d, a, b, w[i +  6], s43, 0xa3014314)
        b = ii(b, c, d, a, w[i + 13], s44, 0x4e0811a1)
        a = ii(a, b, c, d, w[i +  4], s41, 0xf7537e82)
        d = ii(d, a, b, c, w[i + 11], s42, 0xbd3af235)
        c = ii(c, d, a, b, w[i +  2], s43, 0x2ad7d2bb)
        b = ii(b, c, d, a, w[i +  9], s44, 0xeb86d391)
        a = addUnsigned(a, aa)
        b = addUnsigned(b, bb)
        c = addUnsigned(c, cc)
        d = addUnsigned(d, dd)
    }

    return hex(a) + hex(b) + hex(c) + hex(d)

}
Last updated 3 years ago.
nichabi.com