local t={_VERSION="\109\100\053\046\108\117\097 \049\046\049\046\048",_DESCRIPTION="\077\068\053 \099\111\109\112\117\116\097\116\105\111\110 \105\110 \076\117\097 \040\053\046\049-\051\044 \076\117\097\074\073\084\041",_URL="\104\116\116\112\115\058\047\047\103\105\116\104\117\098\046\099\111\109\047\107\105\107\105\116\111\047\109\100\053\046\108\117\097";_LICENSE="    \077\073\084 \076\073\067\069\078\083\069\n\n    \067\111\112\121\114\105\103\104\116 \040\099\041 \050\048\049\051 \069\110\114\105\113\117\101 \071\097\114\099\195\173\097 \067\111\116\097 \043 \065\100\097\109 \066\097\108\100\119\105\110 \043 \104\097\110\122\097\111 \043 \069\113\117\105 \052 \083\111\102\116\119\097\114\101\n\n    \080\101\114\109\105\115\115\105\111\110 \105\115 \104\101\114\101\098\121 \103\114\097\110\116\101\100\044 \102\114\101\101 \111\102 \099\104\097\114\103\101\044 \116\111 \097\110\121 \112\101\114\115\111\110 \111\098\116\097\105\110\105\110\103 \097\n    \099\111\112\121 \111\102 \116\104\105\115 \115\111\102\116\119\097\114\101 \097\110\100 \097\115\115\111\099\105\097\116\101\100 \100\111\099\117\109\101\110\116\097\116\105\111\110 \102\105\108\101\115 \040\116\104\101\n    \"\083\111\102\116\119\097\114\101\"\041\044 \116\111 \100\101\097\108 \105\110 \116\104\101 \083\111\102\116\119\097\114\101 \119\105\116\104\111\117\116 \114\101\115\116\114\105\099\116\105\111\110\044 \105\110\099\108\117\100\105\110\103\n    \119\105\116\104\111\117\116 \108\105\109\105\116\097\116\105\111\110 \116\104\101 \114\105\103\104\116\115 \116\111 \117\115\101\044 \099\111\112\121\044 \109\111\100\105\102\121\044 \109\101\114\103\101\044 \112\117\098\108\105\115\104\044\n    \100\105\115\116\114\105\098\117\116\101\044 \115\117\098\108\105\099\101\110\115\101\044 \097\110\100\047\111\114 \115\101\108\108 \099\111\112\105\101\115 \111\102 \116\104\101 \083\111\102\116\119\097\114\101\044 \097\110\100 \116\111\n    \112\101\114\109\105\116 \112\101\114\115\111\110\115 \116\111 \119\104\111\109 \116\104\101 \083\111\102\116\119\097\114\101 \105\115 \102\117\114\110\105\115\104\101\100 \116\111 \100\111 \115\111\044 \115\117\098\106\101\099\116 \116\111\n    \116\104\101 \102\111\108\108\111\119\105\110\103 \099\111\110\100\105\116\105\111\110\115\058\n\n    \084\104\101 \097\098\111\118\101 \099\111\112\121\114\105\103\104\116 \110\111\116\105\099\101 \097\110\100 \116\104\105\115 \112\101\114\109\105\115\115\105\111\110 \110\111\116\105\099\101 \115\104\097\108\108 \098\101 \105\110\099\108\117\100\101\100\n    \105\110 \097\108\108 \099\111\112\105\101\115 \111\114 \115\117\098\115\116\097\110\116\105\097\108 \112\111\114\116\105\111\110\115 \111\102 \116\104\101 \083\111\102\116\119\097\114\101\046\n\n    \084\072\069 \083\079\070\084\087\065\082\069 \073\083 \080\082\079\086\073\068\069\068 \"\065\083 \073\083\"\044 \087\073\084\072\079\085\084 \087\065\082\082\065\078\084\089 \079\070 \065\078\089 \075\073\078\068\044 \069\088\080\082\069\083\083\n    \079\082 \073\077\080\076\073\069\068\044 \073\078\067\076\085\068\073\078\071 \066\085\084 \078\079\084 \076\073\077\073\084\069\068 \084\079 \084\072\069 \087\065\082\082\065\078\084\073\069\083 \079\070\n    \077\069\082\067\072\065\078\084\065\066\073\076\073\084\089\044 \070\073\084\078\069\083\083 \070\079\082 \065 \080\065\082\084\073\067\085\076\065\082 \080\085\082\080\079\083\069 \065\078\068 \078\079\078\073\078\070\082\073\078\071\069\077\069\078\084\046\n    \073\078 \078\079 \069\086\069\078\084 \083\072\065\076\076 \084\072\069 \065\085\084\072\079\082\083 \079\082 \067\079\080\089\082\073\071\072\084 \072\079\076\068\069\082\083 \066\069 \076\073\065\066\076\069 \070\079\082 \065\078\089\n    \067\076\065\073\077\044 \068\065\077\065\071\069\083 \079\082 \079\084\072\069\082 \076\073\065\066\073\076\073\084\089\044 \087\072\069\084\072\069\082 \073\078 \065\078 \065\067\084\073\079\078 \079\070 \067\079\078\084\082\065\067\084\044\n    \084\079\082\084 \079\082 \079\084\072\069\082\087\073\083\069\044 \065\082\073\083\073\078\071 \070\082\079\077\044 \079\085\084 \079\070 \079\082 \073\078 \067\079\078\078\069\067\084\073\079\078 \087\073\084\072 \084\072\069\n    \083\079\070\084\087\065\082\069 \079\082 \084\072\069 \085\083\069 \079\082 \079\084\072\069\082 \068\069\065\076\073\078\071\083 \073\078 \084\072\069 \083\079\070\084\087\065\082\069\046\n  "}local D,o,s,v,M=string.char,string.byte,string.format,string.rep,string.sub local K,Q,x,f,l,S local Z,z=pcall(require,"\098\105\116")local b,g=pcall(require,"\102\102\105")if Z then K,Q,x,f,l,S=z.bor,z.band,z.bnot,z.bxor,z.rshift,z.lshift else Z,z=pcall(require,"\098\105\116\051\050")if Z then x=z.bnot local t=function(t)return t<=2147483647 and t or-(x(t)+1)end local D=function(D)return function(o,s)return t(D(t(o),t(s)))end end K,Q,f=D(z.bor),D(z.band),D(z.bxor)l,S=D(z.rshift),D(z.lshift)else local function t(t)local D=0 local o=1 for s=1,#t,1 do D=D+t[s]*o o=o*2 end return D end local function D(t,D)local o,s=t,D if#o<#s then o,s=s,o end for t=#s+1,#o,1 do s[t]=0 end end local o x=function(D)local s=o(D)local v=math.max(#s,32)for t=1,v,1 do if s[t]==1 then s[t]=0 else s[t]=1 end end return t(s)end o=function(t)if t<0 then return o(x(math.abs(t))+1)end local D={}local s=1 local v while t>0 do v=t%2 D[s]=v t=(t-v)/2 s=s+1 end return D end K=function(s,v)local M=o(s)local K=o(v)D(M,K)local Q={}for t=1,#M,1 do if M[t]==0 and K[t]==0 then Q[t]=0 else Q[t]=1 end end return t(Q)end Q=function(s,v)local M=o(s)local K=o(v)D(M,K)local Q={}for t=1,#M,1 do if M[t]==0 or K[t]==0 then Q[t]=0 else Q[t]=1 end end return t(Q)end f=function(s,v)local M=o(s)local K=o(v)D(M,K)local Q={}for t=1,#M,1 do if M[t]~=K[t]then Q[t]=1 else Q[t]=0 end end return t(Q)end l=function(t,D)local o=0 if t<0 then t=x(math.abs(t))+1 o=2147483648 end local s=math.floor for D=1,D,1 do t=t/2 t=K(s(t),o)end return s(t)end S=function(t,D)if t<0 then t=x(math.abs(t))+1 end for D=1,D,1 do t=t*2 end return Q(t,4294967295)end end end local i if b then local t=g.typeof("\105\110\116\091\049\093")i=function(D)return g.string(t(D),4)end else i=function(t)local o=function(o)return D(Q(l(t,o),255))end return o(0)..(o(8)..(o(16)..o(24)))end end local function p(t)local D=0 for s=1,#t,1 do D=D*256+o(t,s)end return D end local U if b then local t=g.typeof("\099\111\110\115\116 \099\104\097\114\042")local D=g.typeof("\099\111\110\115\116 \105\110\116\042")U=function(o)local s=t(o)return(g.cast(D,s))[0]end else U=function(t)local D=0 for s=#t,1,-1 do D=D*256+o(t,s)end return D end end local function I(t)return{U(M(t,1,4));U(M(t,5,8));U(M(t,9,12)),U(M(t,13,16)),U(M(t,17,20));U(M(t,21,24));U(M(t,25,28));U(M(t,29,32));U(M(t,33,36));U(M(t,37,40));U(M(t,41,44)),U(M(t,45,48)),U(M(t,49,52));U(M(t,53,56)),U(M(t,57,60));U(M(t,61,64))}end local J={3614090360,3905402710,606105819,3250441966;4118548399;1200080426;2821735955;4249261313;1770035416,2336552879,4294925233,2304563134,1804603682,4254626195;2792965006;1236535329;4129170786,3225465664,643717713;3921069994,3593408605;38016083,3634488961;3889429448;568446438,3275163606;4107603335;1163531501;2850285829,4243563512,1735328473,2368359562;4294588738;2272392833,1839030562,4259657740;2763975236;1272893353,4139469664,3200236656,681279174,3936430074,3572445317;76029189,3654602809,3873151461,530742520;3299628645;4096336452,1126891415;2878612391;4237533241,1700485571,2399980690,4293915773;2240044497,1873313359,4264355552;2734768916;1309151649,4149444226,3174756917,718787259,3951481745;1732584193,4023233417,2562383102,271733878}local X=function(t,D,o)return K(Q(t,D),Q(-t-1,o))end local C=function(t,D,o)return K(Q(t,o),Q(D,-o-1))end local T=function(t,D,o)return f(t,f(D,o))end local d=function(t,D,o)return f(D,K(t,-o-1))end local e=function(t,D,o,s,v,M,x,f)D=Q(((D+t(o,s,v))+M)+f,4294967295)return K(S(Q(D,l(4294967295,x)),x),l(D,32-x))+o end local function q(t,D,o,s,v)local M,K,x,f=t,D,o,s local l=J M=e(X,M,K,x,f,v[0],7,l[1])f=e(X,f,M,K,x,v[1],12,l[2])x=e(X,x,f,M,K,v[2],17,l[3])K=e(X,K,x,f,M,v[3],22,l[4])M=e(X,M,K,x,f,v[4],7,l[5])f=e(X,f,M,K,x,v[5],12,l[6])x=e(X,x,f,M,K,v[6],17,l[7])K=e(X,K,x,f,M,v[7],22,l[8])M=e(X,M,K,x,f,v[8],7,l[9])f=e(X,f,M,K,x,v[9],12,l[10])x=e(X,x,f,M,K,v[10],17,l[11])K=e(X,K,x,f,M,v[11],22,l[12])M=e(X,M,K,x,f,v[12],7,l[13])f=e(X,f,M,K,x,v[13],12,l[14])x=e(X,x,f,M,K,v[14],17,l[15])K=e(X,K,x,f,M,v[15],22,l[16])M=e(C,M,K,x,f,v[1],5,l[17])f=e(C,f,M,K,x,v[6],9,l[18])x=e(C,x,f,M,K,v[11],14,l[19])K=e(C,K,x,f,M,v[0],20,l[20])M=e(C,M,K,x,f,v[5],5,l[21])f=e(C,f,M,K,x,v[10],9,l[22])x=e(C,x,f,M,K,v[15],14,l[23])K=e(C,K,x,f,M,v[4],20,l[24])M=e(C,M,K,x,f,v[9],5,l[25])f=e(C,f,M,K,x,v[14],9,l[26])x=e(C,x,f,M,K,v[3],14,l[27])K=e(C,K,x,f,M,v[8],20,l[28])M=e(C,M,K,x,f,v[13],5,l[29])f=e(C,f,M,K,x,v[2],9,l[30])x=e(C,x,f,M,K,v[7],14,l[31])K=e(C,K,x,f,M,v[12],20,l[32])M=e(T,M,K,x,f,v[5],4,l[33])f=e(T,f,M,K,x,v[8],11,l[34])x=e(T,x,f,M,K,v[11],16,l[35])K=e(T,K,x,f,M,v[14],23,l[36])M=e(T,M,K,x,f,v[1],4,l[37])f=e(T,f,M,K,x,v[4],11,l[38])x=e(T,x,f,M,K,v[7],16,l[39])K=e(T,K,x,f,M,v[10],23,l[40])M=e(T,M,K,x,f,v[13],4,l[41])f=e(T,f,M,K,x,v[0],11,l[42])x=e(T,x,f,M,K,v[3],16,l[43])K=e(T,K,x,f,M,v[6],23,l[44])M=e(T,M,K,x,f,v[9],4,l[45])f=e(T,f,M,K,x,v[12],11,l[46])x=e(T,x,f,M,K,v[15],16,l[47])K=e(T,K,x,f,M,v[2],23,l[48])M=e(d,M,K,x,f,v[0],6,l[49])f=e(d,f,M,K,x,v[7],10,l[50])x=e(d,x,f,M,K,v[14],15,l[51])K=e(d,K,x,f,M,v[5],21,l[52])M=e(d,M,K,x,f,v[12],6,l[53])f=e(d,f,M,K,x,v[3],10,l[54])x=e(d,x,f,M,K,v[10],15,l[55])K=e(d,K,x,f,M,v[1],21,l[56])M=e(d,M,K,x,f,v[8],6,l[57])f=e(d,f,M,K,x,v[15],10,l[58])x=e(d,x,f,M,K,v[6],15,l[59])K=e(d,K,x,f,M,v[13],21,l[60])M=e(d,M,K,x,f,v[4],6,l[61])f=e(d,f,M,K,x,v[11],10,l[62])x=e(d,x,f,M,K,v[2],15,l[63])K=e(d,K,x,f,M,v[9],21,l[64])return Q(t+M,4294967295),Q(D+K,4294967295),Q(o+x,4294967295),Q(s+f,4294967295)end local function h(t,D)t.pos=t.pos+#D D=t.buf..D for o=1,#D-63,64 do local s=I(M(D,o,o+63))assert(#s==16,"\097")s[0]=table.remove(s,1)t.a,t.b,t.c,t.d=q(t.a,t.b,t.c,t.d,s)end t.buf=M(D,math.floor(#D/64)*64+1,#D)return t end local function a(t)local o=t.pos local s=56-o%64 if o%64>56 then s=s+64 end if s==0 then s=64 end local M=D(128)..(v(D(0),s-1)..(i(Q(8*o,4294967295))..i(math.floor(o/536870912))))h(t,M)assert(t.pos%64==0,"")return i(t.a)..(i(t.b)..(i(t.c)..i(t.d)))end function t.new()return{a=J[65];b=J[66];c=J[67];d=J[68];pos=0,buf="",update=h;finish=a}end function t.tohex(t)return s("\037\048\056\120\037\048\056\120\037\048\056\120\037\048\056\120",p(M(t,1,4)),p(M(t,5,8)),p(M(t,9,12)),p(M(t,13,16)))end function t.sum(D)return((t.new()):update(D)):finish()end function t.sumhexa(D)return t.tohex(t.sum(D))end return t
local md5 = {
	_VERSION = "md5.lua 1.1.0",
	_DESCRIPTION = "MD5 computation in Lua (5.1-3, LuaJIT)",
	_URL = "https://github.com/kikito/md5.lua",
	_LICENSE = [[
    MIT LICENSE

    Copyright (c) 2013 Enrique García Cota + Adam Baldwin + hanzao + Equi 4 Software

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  ]],
}

-- bit lib implementions

local char, byte, format, rep, sub = string.char, string.byte, string.format, string.rep, string.sub
local bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift

local ok, bit = pcall(require, "bit")
local ok_ffi, ffi = pcall(require, "ffi")
if ok then
	bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift =
		bit.bor, bit.band, bit.bnot, bit.bxor, bit.rshift, bit.lshift
else
	ok, bit = pcall(require, "bit32")

	if ok then
		bit_not = bit.bnot

		local tobit = function(n)
			return n <= 0x7fffffff and n or -(bit_not(n) + 1)
		end

		local normalize = function(f)
			return function(a, b)
				return tobit(f(tobit(a), tobit(b)))
			end
		end

		bit_or, bit_and, bit_xor = normalize(bit.bor), normalize(bit.band), normalize(bit.bxor)
		bit_rshift, bit_lshift = normalize(bit.rshift), normalize(bit.lshift)
	else
		local function tbl2number(tbl)
			local result = 0
			local power = 1
			for i = 1, #tbl do
				result = result + tbl[i] * power
				power = power * 2
			end
			return result
		end

		local function expand(t1, t2)
			local big, small = t1, t2
			if #big < #small then
				big, small = small, big
			end
			-- expand small
			for i = #small + 1, #big do
				small[i] = 0
			end
		end

		local to_bits -- needs to be declared before bit_not

		bit_not = function(n)
			local tbl = to_bits(n)
			local size = math.max(#tbl, 32)
			for i = 1, size do
				if tbl[i] == 1 then
					tbl[i] = 0
				else
					tbl[i] = 1
				end
			end
			return tbl2number(tbl)
		end

		-- defined as local above
		to_bits = function(n)
			if n < 0 then
				-- negative
				return to_bits(bit_not(math.abs(n)) + 1)
			end
			-- to bits table
			local tbl = {}
			local cnt = 1
			local last
			while n > 0 do
				last = n % 2
				tbl[cnt] = last
				n = (n - last) / 2
				cnt = cnt + 1
			end

			return tbl
		end

		bit_or = function(m, n)
			local tbl_m = to_bits(m)
			local tbl_n = to_bits(n)
			expand(tbl_m, tbl_n)

			local tbl = {}
			for i = 1, #tbl_m do
				if tbl_m[i] == 0 and tbl_n[i] == 0 then
					tbl[i] = 0
				else
					tbl[i] = 1
				end
			end

			return tbl2number(tbl)
		end

		bit_and = function(m, n)
			local tbl_m = to_bits(m)
			local tbl_n = to_bits(n)
			expand(tbl_m, tbl_n)

			local tbl = {}
			for i = 1, #tbl_m do
				if tbl_m[i] == 0 or tbl_n[i] == 0 then
					tbl[i] = 0
				else
					tbl[i] = 1
				end
			end

			return tbl2number(tbl)
		end

		bit_xor = function(m, n)
			local tbl_m = to_bits(m)
			local tbl_n = to_bits(n)
			expand(tbl_m, tbl_n)

			local tbl = {}
			for i = 1, #tbl_m do
				if tbl_m[i] ~= tbl_n[i] then
					tbl[i] = 1
				else
					tbl[i] = 0
				end
			end

			return tbl2number(tbl)
		end

		bit_rshift = function(n, bits)
			local high_bit = 0
			if n < 0 then
				-- negative
				n = bit_not(math.abs(n)) + 1
				high_bit = 0x80000000
			end

			local floor = math.floor

			for _ = 1, bits do
				n = n / 2
				n = bit_or(floor(n), high_bit)
			end
			return floor(n)
		end

		bit_lshift = function(n, bits)
			if n < 0 then
				-- negative
				n = bit_not(math.abs(n)) + 1
			end

			for _ = 1, bits do
				n = n * 2
			end
			return bit_and(n, 0xFFFFFFFF)
		end
	end
end

-- convert little-endian 32-bit int to a 4-char string
local lei2str
-- function is defined this way to allow full jit compilation (removing UCLO instruction in LuaJIT)
if ok_ffi then
	local ct_IntType = ffi.typeof("int[1]")
	lei2str = function(i)
		return ffi.string(ct_IntType(i), 4)
	end
else
	lei2str = function(i)
		local f = function(s)
			return char(bit_and(bit_rshift(i, s), 255))
		end
		return f(0) .. f(8) .. f(16) .. f(24)
	end
end

-- convert raw string to big-endian int
local function str2bei(s)
	local v = 0
	for i = 1, #s do
		v = v * 256 + byte(s, i)
	end
	return v
end

-- convert raw string to little-endian int
local str2lei

if ok_ffi then
	local ct_constcharptr = ffi.typeof("const char*")
	local ct_constintptr = ffi.typeof("const int*")
	str2lei = function(s)
		local int = ct_constcharptr(s)
		return ffi.cast(ct_constintptr, int)[0]
	end
else
	str2lei = function(s)
		local v = 0
		for i = #s, 1, -1 do
			v = v * 256 + byte(s, i)
		end
		return v
	end
end

-- cut up a string in little-endian ints of given size
local function cut_le_str(s)
	return {
		str2lei(sub(s, 1, 4)),
		str2lei(sub(s, 5, 8)),
		str2lei(sub(s, 9, 12)),
		str2lei(sub(s, 13, 16)),
		str2lei(sub(s, 17, 20)),
		str2lei(sub(s, 21, 24)),
		str2lei(sub(s, 25, 28)),
		str2lei(sub(s, 29, 32)),
		str2lei(sub(s, 33, 36)),
		str2lei(sub(s, 37, 40)),
		str2lei(sub(s, 41, 44)),
		str2lei(sub(s, 45, 48)),
		str2lei(sub(s, 49, 52)),
		str2lei(sub(s, 53, 56)),
		str2lei(sub(s, 57, 60)),
		str2lei(sub(s, 61, 64)),
	}
end

-- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh)
-- 10/02/2001 jcw@equi4.com

local CONSTS = {
	0xd76aa478,
	0xe8c7b756,
	0x242070db,
	0xc1bdceee,
	0xf57c0faf,
	0x4787c62a,
	0xa8304613,
	0xfd469501,
	0x698098d8,
	0x8b44f7af,
	0xffff5bb1,
	0x895cd7be,
	0x6b901122,
	0xfd987193,
	0xa679438e,
	0x49b40821,
	0xf61e2562,
	0xc040b340,
	0x265e5a51,
	0xe9b6c7aa,
	0xd62f105d,
	0x02441453,
	0xd8a1e681,
	0xe7d3fbc8,
	0x21e1cde6,
	0xc33707d6,
	0xf4d50d87,
	0x455a14ed,
	0xa9e3e905,
	0xfcefa3f8,
	0x676f02d9,
	0x8d2a4c8a,
	0xfffa3942,
	0x8771f681,
	0x6d9d6122,
	0xfde5380c,
	0xa4beea44,
	0x4bdecfa9,
	0xf6bb4b60,
	0xbebfbc70,
	0x289b7ec6,
	0xeaa127fa,
	0xd4ef3085,
	0x04881d05,
	0xd9d4d039,
	0xe6db99e5,
	0x1fa27cf8,
	0xc4ac5665,
	0xf4292244,
	0x432aff97,
	0xab9423a7,
	0xfc93a039,
	0x655b59c3,
	0x8f0ccc92,
	0xffeff47d,
	0x85845dd1,
	0x6fa87e4f,
	0xfe2ce6e0,
	0xa3014314,
	0x4e0811a1,
	0xf7537e82,
	0xbd3af235,
	0x2ad7d2bb,
	0xeb86d391,
	0x67452301,
	0xefcdab89,
	0x98badcfe,
	0x10325476,
}

local f = function(x, y, z)
	return bit_or(bit_and(x, y), bit_and(-x - 1, z))
end
local g = function(x, y, z)
	return bit_or(bit_and(x, z), bit_and(y, -z - 1))
end
local h = function(x, y, z)
	return bit_xor(x, bit_xor(y, z))
end
local i = function(x, y, z)
	return bit_xor(y, bit_or(x, -z - 1))
end
local z = function(ff, a, b, c, d, x, s, ac)
	a = bit_and(a + ff(b, c, d) + x + ac, 0xFFFFFFFF)
	-- be *very* careful that left shift does not cause rounding!
	return bit_or(bit_lshift(bit_and(a, bit_rshift(0xFFFFFFFF, s)), s), bit_rshift(a, 32 - s)) + b
end

local function transform(A, B, C, D, X)
	local a, b, c, d = A, B, C, D
	local t = CONSTS

	a = z(f, a, b, c, d, X[0], 7, t[1])
	d = z(f, d, a, b, c, X[1], 12, t[2])
	c = z(f, c, d, a, b, X[2], 17, t[3])
	b = z(f, b, c, d, a, X[3], 22, t[4])
	a = z(f, a, b, c, d, X[4], 7, t[5])
	d = z(f, d, a, b, c, X[5], 12, t[6])
	c = z(f, c, d, a, b, X[6], 17, t[7])
	b = z(f, b, c, d, a, X[7], 22, t[8])
	a = z(f, a, b, c, d, X[8], 7, t[9])
	d = z(f, d, a, b, c, X[9], 12, t[10])
	c = z(f, c, d, a, b, X[10], 17, t[11])
	b = z(f, b, c, d, a, X[11], 22, t[12])
	a = z(f, a, b, c, d, X[12], 7, t[13])
	d = z(f, d, a, b, c, X[13], 12, t[14])
	c = z(f, c, d, a, b, X[14], 17, t[15])
	b = z(f, b, c, d, a, X[15], 22, t[16])

	a = z(g, a, b, c, d, X[1], 5, t[17])
	d = z(g, d, a, b, c, X[6], 9, t[18])
	c = z(g, c, d, a, b, X[11], 14, t[19])
	b = z(g, b, c, d, a, X[0], 20, t[20])
	a = z(g, a, b, c, d, X[5], 5, t[21])
	d = z(g, d, a, b, c, X[10], 9, t[22])
	c = z(g, c, d, a, b, X[15], 14, t[23])
	b = z(g, b, c, d, a, X[4], 20, t[24])
	a = z(g, a, b, c, d, X[9], 5, t[25])
	d = z(g, d, a, b, c, X[14], 9, t[26])
	c = z(g, c, d, a, b, X[3], 14, t[27])
	b = z(g, b, c, d, a, X[8], 20, t[28])
	a = z(g, a, b, c, d, X[13], 5, t[29])
	d = z(g, d, a, b, c, X[2], 9, t[30])
	c = z(g, c, d, a, b, X[7], 14, t[31])
	b = z(g, b, c, d, a, X[12], 20, t[32])

	a = z(h, a, b, c, d, X[5], 4, t[33])
	d = z(h, d, a, b, c, X[8], 11, t[34])
	c = z(h, c, d, a, b, X[11], 16, t[35])
	b = z(h, b, c, d, a, X[14], 23, t[36])
	a = z(h, a, b, c, d, X[1], 4, t[37])
	d = z(h, d, a, b, c, X[4], 11, t[38])
	c = z(h, c, d, a, b, X[7], 16, t[39])
	b = z(h, b, c, d, a, X[10], 23, t[40])
	a = z(h, a, b, c, d, X[13], 4, t[41])
	d = z(h, d, a, b, c, X[0], 11, t[42])
	c = z(h, c, d, a, b, X[3], 16, t[43])
	b = z(h, b, c, d, a, X[6], 23, t[44])
	a = z(h, a, b, c, d, X[9], 4, t[45])
	d = z(h, d, a, b, c, X[12], 11, t[46])
	c = z(h, c, d, a, b, X[15], 16, t[47])
	b = z(h, b, c, d, a, X[2], 23, t[48])

	a = z(i, a, b, c, d, X[0], 6, t[49])
	d = z(i, d, a, b, c, X[7], 10, t[50])
	c = z(i, c, d, a, b, X[14], 15, t[51])
	b = z(i, b, c, d, a, X[5], 21, t[52])
	a = z(i, a, b, c, d, X[12], 6, t[53])
	d = z(i, d, a, b, c, X[3], 10, t[54])
	c = z(i, c, d, a, b, X[10], 15, t[55])
	b = z(i, b, c, d, a, X[1], 21, t[56])
	a = z(i, a, b, c, d, X[8], 6, t[57])
	d = z(i, d, a, b, c, X[15], 10, t[58])
	c = z(i, c, d, a, b, X[6], 15, t[59])
	b = z(i, b, c, d, a, X[13], 21, t[60])
	a = z(i, a, b, c, d, X[4], 6, t[61])
	d = z(i, d, a, b, c, X[11], 10, t[62])
	c = z(i, c, d, a, b, X[2], 15, t[63])
	b = z(i, b, c, d, a, X[9], 21, t[64])

	return bit_and(A + a, 0xFFFFFFFF),
		bit_and(B + b, 0xFFFFFFFF),
		bit_and(C + c, 0xFFFFFFFF),
		bit_and(D + d, 0xFFFFFFFF)
end

----------------------------------------------------------------

local function md5_update(self, s)
	self.pos = self.pos + #s
	s = self.buf .. s
	for ii = 1, #s - 63, 64 do
		local X = cut_le_str(sub(s, ii, ii + 63))
		assert(#X == 16, "a")
		X[0] = table.remove(X, 1) -- zero based!
		self.a, self.b, self.c, self.d = transform(self.a, self.b, self.c, self.d, X)
	end
	self.buf = sub(s, math.floor(#s / 64) * 64 + 1, #s)
	return self
end

local function md5_finish(self)
	local msgLen = self.pos
	local padLen = 56 - msgLen % 64

	if msgLen % 64 > 56 then
		padLen = padLen + 64
	end

	if padLen == 0 then
		padLen = 64
	end

	local s = char(128)
		.. rep(char(0), padLen - 1)
		.. lei2str(bit_and(8 * msgLen, 0xFFFFFFFF))
		.. lei2str(math.floor(msgLen / 0x20000000))
	md5_update(self, s)

	assert(self.pos % 64 == 0, "")
	return lei2str(self.a) .. lei2str(self.b) .. lei2str(self.c) .. lei2str(self.d)
end

----------------------------------------------------------------

function md5.new()
	return {
		a = CONSTS[65],
		b = CONSTS[66],
		c = CONSTS[67],
		d = CONSTS[68],
		pos = 0,
		buf = "",
		update = md5_update,
		finish = md5_finish,
	}
end

function md5.tohex(s)
	return format(
		"%08x%08x%08x%08x",
		str2bei(sub(s, 1, 4)),
		str2bei(sub(s, 5, 8)),
		str2bei(sub(s, 9, 12)),
		str2bei(sub(s, 13, 16))
	)
end

function md5.sum(s)
	return md5.new():update(s):finish()
end

function md5.sumhexa(s)
	return md5.tohex(md5.sum(s))
end

return md5
