2 _VERSION
= "md5.lua 0.5.0",
3 _DESCRIPTION
= "MD5 computation in Lua (5.1)",
4 _URL
= "https://github.com/kikito/md5.lua",
8 Copyright (c) 2013 Enrique GarcĂa Cota + Adam Baldwin + hanzao + Equi 4 Software
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice shall be included
19 in all copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 -- bit lib implementions
33 local floor, abs, max = math
.floor, math
.abs, math
.max
34 local char
, byte
, format, rep
, sub
=
35 string.char
, string.byte
, string.format, string.rep
, string.sub
37 local function check_int(n
)
39 if(n
- floor(n
) > 0) then
40 error("trying to use bitwise operation on non-integer!")
44 local function tbl2number(tbl
)
50 rslt
= rslt
+ tbl
[i
]*power
57 local function expand(tbl_m
, tbl_n
)
60 if(#tbl_m
> #tbl_n
) then
68 for i
= #small
+ 1, #big
do
74 local to_bits
-- needs to be declared before bit_not
76 local function bit_not(n
)
77 local tbl
= to_bits(n
)
78 local size
= max(#tbl
, 32)
86 return tbl2number(tbl
)
89 -- defined as local above
90 to_bits
= function (n
)
94 return to_bits(bit_not(abs(n
)) + 1)
100 local last
= math
.fmod(n
,2)
113 local function bit_or(m
, n
)
114 local tbl_m
= to_bits(m
)
115 local tbl_n
= to_bits(n
)
119 local rslt
= max(#tbl_m
, #tbl_n
)
121 if(tbl_m
[i
]== 0 and tbl_n
[i
] == 0) then
128 return tbl2number(tbl
)
131 local function bit_and(m
, n
)
132 local tbl_m
= to_bits(m
)
133 local tbl_n
= to_bits(n
)
137 local rslt
= max(#tbl_m
, #tbl_n
)
139 if(tbl_m
[i
]== 0 or tbl_n
[i
] == 0) then
146 return tbl2number(tbl
)
149 local function bit_xor(m
, n
)
150 local tbl_m
= to_bits(m
)
151 local tbl_n
= to_bits(n
)
155 local rslt
= max(#tbl_m
, #tbl_n
)
157 if(tbl_m
[i
] ~= tbl_n
[i
]) then
164 return tbl2number(tbl
)
167 local function bit_rshift(n
, bits
)
173 n
= bit_not(abs(n
)) + 1
174 high_bit
= 2147483648 -- 0x80000000
179 n
= bit_or(floor(n
), high_bit
)
184 local function bit_lshift(n
, bits
)
189 n
= bit_not(abs(n
)) + 1
195 return bit_and(n
, 4294967295) -- 0xFFFFFFFF
198 -- convert little-endian 32-bit int to a 4-char string
199 local function lei2str(i
)
200 local f
=function (s
) return char( bit_and( bit_rshift(i
, s
), 255)) end
201 return f(0)..f(8)..f(16)..f(24)
204 -- convert raw string to big-endian int
205 local function str2bei(s
)
208 v
= v
* 256 + byte(s
, i
)
213 -- convert raw string to little-endian int
214 local function str2lei(s
)
217 v
= v
*256 + byte(s
, i
)
222 -- cut up a string in little-endian ints of given size
223 local function cut_le_str(s
,...)
227 table.insert(r
, str2lei(sub(s
, o
, o
+ args
[i
] - 1)))
233 local swap
= function (w
) return str2bei(lei2str(w
)) end
235 local function hex2binaryaux(hexval
)
236 return char(tonumber(hexval
, 16))
239 local function hex2binary(hex
)
240 local result
, _
= hex
:gsub('..', hex2binaryaux
)
244 -- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh)
245 -- 10/02/2001 jcw@equi4.com
247 local FF
= 0xffffffff
249 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
250 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
251 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
252 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
253 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
254 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
255 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
256 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
257 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
258 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
259 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
260 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
261 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
262 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
263 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
264 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
265 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476
268 local f
=function (x
,y
,z
) return bit_or(bit_and(x
,y
),bit_and(-x
-1,z
)) end
269 local g
=function (x
,y
,z
) return bit_or(bit_and(x
,z
),bit_and(y
,-z
-1)) end
270 local h
=function (x
,y
,z
) return bit_xor(x
,bit_xor(y
,z
)) end
271 local i
=function (x
,y
,z
) return bit_xor(y
,bit_or(x
,-z
-1)) end
272 local z
=function (f
,a
,b
,c
,d
,x
,s
,ac
)
273 a
=bit_and(a
+f(b
,c
,d
)+x
+ac
,FF
)
274 -- be *very* careful that left shift does not cause rounding!
275 return bit_or(bit_lshift(bit_and(a
,bit_rshift(FF
,s
)),s
),bit_rshift(a
,32-s
))+b
278 local function transform(A
,B
,C
,D
,X
)
279 local a
,b
,c
,d
=A
,B
,C
,D
282 a
=z(f
,a
,b
,c
,d
,X
[ 0], 7,t
[ 1])
283 d
=z(f
,d
,a
,b
,c
,X
[ 1],12,t
[ 2])
284 c
=z(f
,c
,d
,a
,b
,X
[ 2],17,t
[ 3])
285 b
=z(f
,b
,c
,d
,a
,X
[ 3],22,t
[ 4])
286 a
=z(f
,a
,b
,c
,d
,X
[ 4], 7,t
[ 5])
287 d
=z(f
,d
,a
,b
,c
,X
[ 5],12,t
[ 6])
288 c
=z(f
,c
,d
,a
,b
,X
[ 6],17,t
[ 7])
289 b
=z(f
,b
,c
,d
,a
,X
[ 7],22,t
[ 8])
290 a
=z(f
,a
,b
,c
,d
,X
[ 8], 7,t
[ 9])
291 d
=z(f
,d
,a
,b
,c
,X
[ 9],12,t
[10])
292 c
=z(f
,c
,d
,a
,b
,X
[10],17,t
[11])
293 b
=z(f
,b
,c
,d
,a
,X
[11],22,t
[12])
294 a
=z(f
,a
,b
,c
,d
,X
[12], 7,t
[13])
295 d
=z(f
,d
,a
,b
,c
,X
[13],12,t
[14])
296 c
=z(f
,c
,d
,a
,b
,X
[14],17,t
[15])
297 b
=z(f
,b
,c
,d
,a
,X
[15],22,t
[16])
299 a
=z(g
,a
,b
,c
,d
,X
[ 1], 5,t
[17])
300 d
=z(g
,d
,a
,b
,c
,X
[ 6], 9,t
[18])
301 c
=z(g
,c
,d
,a
,b
,X
[11],14,t
[19])
302 b
=z(g
,b
,c
,d
,a
,X
[ 0],20,t
[20])
303 a
=z(g
,a
,b
,c
,d
,X
[ 5], 5,t
[21])
304 d
=z(g
,d
,a
,b
,c
,X
[10], 9,t
[22])
305 c
=z(g
,c
,d
,a
,b
,X
[15],14,t
[23])
306 b
=z(g
,b
,c
,d
,a
,X
[ 4],20,t
[24])
307 a
=z(g
,a
,b
,c
,d
,X
[ 9], 5,t
[25])
308 d
=z(g
,d
,a
,b
,c
,X
[14], 9,t
[26])
309 c
=z(g
,c
,d
,a
,b
,X
[ 3],14,t
[27])
310 b
=z(g
,b
,c
,d
,a
,X
[ 8],20,t
[28])
311 a
=z(g
,a
,b
,c
,d
,X
[13], 5,t
[29])
312 d
=z(g
,d
,a
,b
,c
,X
[ 2], 9,t
[30])
313 c
=z(g
,c
,d
,a
,b
,X
[ 7],14,t
[31])
314 b
=z(g
,b
,c
,d
,a
,X
[12],20,t
[32])
316 a
=z(h
,a
,b
,c
,d
,X
[ 5], 4,t
[33])
317 d
=z(h
,d
,a
,b
,c
,X
[ 8],11,t
[34])
318 c
=z(h
,c
,d
,a
,b
,X
[11],16,t
[35])
319 b
=z(h
,b
,c
,d
,a
,X
[14],23,t
[36])
320 a
=z(h
,a
,b
,c
,d
,X
[ 1], 4,t
[37])
321 d
=z(h
,d
,a
,b
,c
,X
[ 4],11,t
[38])
322 c
=z(h
,c
,d
,a
,b
,X
[ 7],16,t
[39])
323 b
=z(h
,b
,c
,d
,a
,X
[10],23,t
[40])
324 a
=z(h
,a
,b
,c
,d
,X
[13], 4,t
[41])
325 d
=z(h
,d
,a
,b
,c
,X
[ 0],11,t
[42])
326 c
=z(h
,c
,d
,a
,b
,X
[ 3],16,t
[43])
327 b
=z(h
,b
,c
,d
,a
,X
[ 6],23,t
[44])
328 a
=z(h
,a
,b
,c
,d
,X
[ 9], 4,t
[45])
329 d
=z(h
,d
,a
,b
,c
,X
[12],11,t
[46])
330 c
=z(h
,c
,d
,a
,b
,X
[15],16,t
[47])
331 b
=z(h
,b
,c
,d
,a
,X
[ 2],23,t
[48])
333 a
=z(i
,a
,b
,c
,d
,X
[ 0], 6,t
[49])
334 d
=z(i
,d
,a
,b
,c
,X
[ 7],10,t
[50])
335 c
=z(i
,c
,d
,a
,b
,X
[14],15,t
[51])
336 b
=z(i
,b
,c
,d
,a
,X
[ 5],21,t
[52])
337 a
=z(i
,a
,b
,c
,d
,X
[12], 6,t
[53])
338 d
=z(i
,d
,a
,b
,c
,X
[ 3],10,t
[54])
339 c
=z(i
,c
,d
,a
,b
,X
[10],15,t
[55])
340 b
=z(i
,b
,c
,d
,a
,X
[ 1],21,t
[56])
341 a
=z(i
,a
,b
,c
,d
,X
[ 8], 6,t
[57])
342 d
=z(i
,d
,a
,b
,c
,X
[15],10,t
[58])
343 c
=z(i
,c
,d
,a
,b
,X
[ 6],15,t
[59])
344 b
=z(i
,b
,c
,d
,a
,X
[13],21,t
[60])
345 a
=z(i
,a
,b
,c
,d
,X
[ 4], 6,t
[61])
346 d
=z(i
,d
,a
,b
,c
,X
[11],10,t
[62])
347 c
=z(i
,c
,d
,a
,b
,X
[ 2],15,t
[63])
348 b
=z(i
,b
,c
,d
,a
,X
[ 9],21,t
[64])
350 return A
+a
,B
+b
,C
+c
,D
+d
353 ----------------------------------------------------------------
355 function md5
.sumhexa(s
)
357 local padLen
= 56 - msgLen
% 64
359 if msgLen
% 64 > 56 then padLen
= padLen
+ 64 end
361 if padLen
== 0 then padLen
= 64 end
363 s
= s
.. char(128) .. rep(char(0),padLen
-1) .. lei2str(8*msgLen
) .. lei2str(0)
368 local a
,b
,c
,d
= t
[65],t
[66],t
[67],t
[68]
371 local X
= cut_le_str(sub(s
,i
,i
+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
373 X
[0] = table.remove(X
,1) -- zero based!
374 a
,b
,c
,d
= transform(a
,b
,c
,d
,X
)
377 return format("%08x%08x%08x%08x",swap(a
),swap(b
),swap(c
),swap(d
))
381 return hex2binary(md5
.sumhexa(s
))