-- from https://github.com/philanc/plc/blob/master/plc/md5.lua ------------------------------------------------------------------------ -- md5 hash - see RFC 1321 - https://www.ietf.org/rfc/rfc1321.txt local spack, sunpack = string.pack, string.unpack ------------------------------------------------------------------------ local function FF(a, b, c, d, x, s, ac) a = (a + ((b & c) | ((~b) & d)) + x + ac) & 0xffffffff a = ((a << s) | (a >> (32-s))) & 0xffffffff a = (a + b) & 0xffffffff return a end local function GG(a, b, c, d, x, s, ac) a = (a + ((b & d) | c & (~d) ) + x + ac) & 0xffffffff a = ((a << s) | (a >> (32-s))) & 0xffffffff a = (a + b) & 0xffffffff return a end local function HH(a, b, c, d, x, s, ac) a = (a + ((b ~ c ~ d)) + x + ac) & 0xffffffff a = ((a << s) | (a >> (32-s))) & 0xffffffff a = (a + b) & 0xffffffff return a end local function II(a, b, c, d, x, s, ac) a = (a + (c ~ (b | ~d)) + x + ac) & 0xffffffff a = ((a << s) | (a >> (32-s))) & 0xffffffff a = (a + b) & 0xffffffff return a end local function transform(state, input, i, t) -- process the 64-byte input block in string 'input' at offset 'i' -- t is a uint32[16] array. It is passed as a parameter -- for performance reasons -- local a, b, c, d = state[1], state[2], state[3], state[4] -- load array for j = 1, 16 do t[j] = sunpack("= 64 do -- process block transform(state, input, i, ibt) i = i + 64 -- update input index r = r - 64 -- update number of unprocessed bytes end -- finalize. must append to input a mandatory 0x80 byte, some -- padding, and the input bit-length ('inputbits') local lastblock -- the rest of input .. some padding .. inputbits local padlen -- padding length in bytes if r < 56 then padlen = 55 - r else padlen = 119 - r end lastblock = input:sub(i) -- remaining input .. '\x80' .. ('\0'):rep(padlen) --padding .. spack("