forked from minetest-mods/MoreMesecons
Merge branch 'pgimeno-incremental-md5'
This commit is contained in:
commit
172176713e
58
md5.lua
58
md5.lua
@ -232,15 +232,6 @@ end
|
|||||||
|
|
||||||
local swap = function (w) return str2bei(lei2str(w)) end
|
local swap = function (w) return str2bei(lei2str(w)) end
|
||||||
|
|
||||||
local function hex2binaryaux(hexval)
|
|
||||||
return char(tonumber(hexval, 16))
|
|
||||||
end
|
|
||||||
|
|
||||||
local function hex2binary(hex)
|
|
||||||
local result, _ = hex:gsub('..', hex2binaryaux)
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
-- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh)
|
-- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh)
|
||||||
-- 10/02/2001 jcw@equi4.com
|
-- 10/02/2001 jcw@equi4.com
|
||||||
|
|
||||||
@ -352,33 +343,54 @@ end
|
|||||||
|
|
||||||
----------------------------------------------------------------
|
----------------------------------------------------------------
|
||||||
|
|
||||||
function md5.sumhexa(s)
|
local function md5_update(self, s)
|
||||||
local msgLen = #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),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
|
||||||
|
assert(#X == 16)
|
||||||
|
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
|
local padLen = 56 - msgLen % 64
|
||||||
|
|
||||||
if msgLen % 64 > 56 then padLen = padLen + 64 end
|
if msgLen % 64 > 56 then padLen = padLen + 64 end
|
||||||
|
|
||||||
if padLen == 0 then padLen = 64 end
|
if padLen == 0 then padLen = 64 end
|
||||||
|
|
||||||
s = s .. char(128) .. rep(char(0),padLen-1) .. lei2str(8*msgLen) .. lei2str(0)
|
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(#s % 64 == 0)
|
assert(self.pos % 64 == 0)
|
||||||
|
return lei2str(self.a) .. lei2str(self.b) .. lei2str(self.c) .. lei2str(self.d)
|
||||||
|
end
|
||||||
|
|
||||||
local t = CONSTS
|
----------------------------------------------------------------
|
||||||
local a,b,c,d = t[65],t[66],t[67],t[68]
|
|
||||||
|
|
||||||
for ii=1,#s,64 do
|
function md5.new()
|
||||||
local X = cut_le_str(sub(s,ii,ii+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
|
return { a = CONSTS[65], b = CONSTS[66], c = CONSTS[67], d = CONSTS[68],
|
||||||
assert(#X == 16)
|
pos = 0,
|
||||||
X[0] = table.remove(X,1) -- zero based!
|
buf = '',
|
||||||
a,b,c,d = transform(a,b,c,d,X)
|
update = md5_update,
|
||||||
end
|
finish = md5_finish }
|
||||||
|
end
|
||||||
|
|
||||||
return format("%08x%08x%08x%08x",swap(a),swap(b),swap(c),swap(d))
|
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
|
end
|
||||||
|
|
||||||
function md5.sum(s)
|
function md5.sum(s)
|
||||||
return hex2binary(md5.sumhexa(s))
|
return md5.new():update(s):finish()
|
||||||
|
end
|
||||||
|
|
||||||
|
function md5.sumhexa(s)
|
||||||
|
return md5.tohex(md5.sum(s))
|
||||||
end
|
end
|
||||||
|
|
||||||
return md5
|
return md5
|
||||||
|
@ -16,6 +16,10 @@ describe('md5', function()
|
|||||||
assert.equal(md5.sumhexa(''), 'd41d8cd98f00b204e9800998ecf8427e')
|
assert.equal(md5.sumhexa(''), 'd41d8cd98f00b204e9800998ecf8427e')
|
||||||
assert.equal(md5.sumhexa(('1'):rep(824)), 'a126fd3611ab8d9b7e8a3384e2fa78a0')
|
assert.equal(md5.sumhexa(('1'):rep(824)), 'a126fd3611ab8d9b7e8a3384e2fa78a0')
|
||||||
assert.equal(md5.sumhexa(('1'):rep(1528)), '3750b6a29d923b633e05d6ae76895664')
|
assert.equal(md5.sumhexa(('1'):rep(1528)), '3750b6a29d923b633e05d6ae76895664')
|
||||||
|
local state = md5.new()
|
||||||
|
state:update('Hello')
|
||||||
|
state:update(', World!')
|
||||||
|
assert.equal(md5.tohex(state:finish()), '65a8e27d8879283831b664bd8b7f0ad4')
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@ -27,6 +31,10 @@ describe('md5', function()
|
|||||||
assert.equal(md5.sum(''), hex2bin 'd41d8cd98f00b204e9800998ecf8427e')
|
assert.equal(md5.sum(''), hex2bin 'd41d8cd98f00b204e9800998ecf8427e')
|
||||||
assert.equal(md5.sum(('1'):rep(824)), hex2bin 'a126fd3611ab8d9b7e8a3384e2fa78a0')
|
assert.equal(md5.sum(('1'):rep(824)), hex2bin 'a126fd3611ab8d9b7e8a3384e2fa78a0')
|
||||||
assert.equal(md5.sum(('1'):rep(1528)), hex2bin '3750b6a29d923b633e05d6ae76895664')
|
assert.equal(md5.sum(('1'):rep(1528)), hex2bin '3750b6a29d923b633e05d6ae76895664')
|
||||||
|
local state = md5.new()
|
||||||
|
state:update('Hello')
|
||||||
|
state:update(', World!')
|
||||||
|
assert.equal(state:finish(), hex2bin '65a8e27d8879283831b664bd8b7f0ad4')
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user