Updated MD5.lua

Now it's the same as the current master branch in kikito's repo
This commit is contained in:
Pablo Mayobre 2015-02-09 15:58:49 -03:00
parent d1b8131693
commit d740129717

View File

@ -1,6 +1,6 @@
local md5 = { local md5 = {
_VERSION = "md5.lua 0.5.0", _VERSION = "md5.lua 1.0.0",
_DESCRIPTION = "MD5 computation in Lua (5.1)", _DESCRIPTION = "MD5 computation in Lua (5.1-3, LuaJIT)",
_URL = "https://github.com/kikito/md5.lua", _URL = "https://github.com/kikito/md5.lua",
_LICENSE = [[ _LICENSE = [[
MIT LICENSE MIT LICENSE
@ -35,8 +35,8 @@ local char, byte, format, rep, sub =
string.char, string.byte, string.format, string.rep, string.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 bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift
local ok, bit = pcall(require, "bit") local ok, bit = pcall(require, 'bit')
if not ok then ok, bit = pcall(require, "bit32") end if not ok then ok, bit = pcall(require, 'bit32') end
if ok then if ok then
bit_or, bit_and, bit_not, bit_xor = bit.bor, bit.band, bit.bnot, bit.bxor bit_or, bit_and, bit_not, bit_xor = bit.bor, bit.band, bit.bnot, bit.bxor
@ -48,20 +48,20 @@ else
error("trying to use bitwise operation on non-integer!") error("trying to use bitwise operation on non-integer!")
end end
end end
local function tbl2number(tbl) local function tbl2number(tbl)
local n = #tbl local n = #tbl
local rslt = 0 local rslt = 0
local power = 1 local power = 1
for i = 1, n do for i = 1, n do
rslt = rslt + tbl[i]*power rslt = rslt + tbl[i]*power
power = power*2 power = power*2
end end
return rslt return rslt
end end
local function expand(tbl_m, tbl_n) local function expand(tbl_m, tbl_n)
local big = {} local big = {}
local small = {} local small = {}
@ -76,11 +76,11 @@ else
for i = #small + 1, #big do for i = #small + 1, #big do
small[i] = 0 small[i] = 0
end end
end end
local to_bits -- needs to be declared before bit_not local to_bits -- needs to be declared before bit_not
function bit_not(n) function bit_not(n)
local tbl = to_bits(n) local tbl = to_bits(n)
local size = max(#tbl, 32) local size = max(#tbl, 32)
@ -93,7 +93,7 @@ else
end end
return tbl2number(tbl) return tbl2number(tbl)
end end
-- defined as local above -- defined as local above
to_bits = function (n) to_bits = function (n)
check_int(n) check_int(n)
@ -114,15 +114,15 @@ else
n = (n-last)/2 n = (n-last)/2
cnt = cnt + 1 cnt = cnt + 1
end end
return tbl return tbl
end end
function bit_or(m, n) function bit_or(m, n)
local tbl_m = to_bits(m) local tbl_m = to_bits(m)
local tbl_n = to_bits(n) local tbl_n = to_bits(n)
expand(tbl_m, tbl_n) expand(tbl_m, tbl_n)
local tbl = {} local tbl = {}
local rslt = max(#tbl_m, #tbl_n) local rslt = max(#tbl_m, #tbl_n)
for i = 1, rslt do for i = 1, rslt do
@ -132,15 +132,15 @@ else
tbl[i] = 1 tbl[i] = 1
end end
end end
return tbl2number(tbl) return tbl2number(tbl)
end end
function bit_and(m, n) function bit_and(m, n)
local tbl_m = to_bits(m) local tbl_m = to_bits(m)
local tbl_n = to_bits(n) local tbl_n = to_bits(n)
expand(tbl_m, tbl_n) expand(tbl_m, tbl_n)
local tbl = {} local tbl = {}
local rslt = max(#tbl_m, #tbl_n) local rslt = max(#tbl_m, #tbl_n)
for i = 1, rslt do for i = 1, rslt do
@ -150,15 +150,15 @@ else
tbl[i] = 1 tbl[i] = 1
end end
end end
return tbl2number(tbl) return tbl2number(tbl)
end end
function bit_xor(m, n) function bit_xor(m, n)
local tbl_m = to_bits(m) local tbl_m = to_bits(m)
local tbl_n = to_bits(n) local tbl_n = to_bits(n)
expand(tbl_m, tbl_n) expand(tbl_m, tbl_n)
local tbl = {} local tbl = {}
local rslt = max(#tbl_m, #tbl_n) local rslt = max(#tbl_m, #tbl_n)
for i = 1, rslt do for i = 1, rslt do
@ -168,35 +168,35 @@ else
tbl[i] = 0 tbl[i] = 0
end end
end end
return tbl2number(tbl) return tbl2number(tbl)
end end
function bit_rshift(n, bits) function bit_rshift(n, bits)
check_int(n) check_int(n)
local high_bit = 0 local high_bit = 0
if(n < 0) then if(n < 0) then
-- negative -- negative
n = bit_not(abs(n)) + 1 n = bit_not(abs(n)) + 1
high_bit = 2147483648 -- 0x80000000 high_bit = 2147483648 -- 0x80000000
end end
for i=1, bits do for i=1, bits do
n = n/2 n = n/2
n = bit_or(floor(n), high_bit) n = bit_or(floor(n), high_bit)
end end
return floor(n) return floor(n)
end end
function bit_lshift(n, bits) function bit_lshift(n, bits)
check_int(n) check_int(n)
if(n < 0) then if(n < 0) then
-- negative -- negative
n = bit_not(abs(n)) + 1 n = bit_not(abs(n)) + 1
end end
for i=1, bits do for i=1, bits do
n = n*2 n = n*2
end end