Skip to content

Commit

Permalink
优化hmac
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyi0226 committed Oct 27, 2015
1 parent f5740e0 commit 1999a03
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lualib/md5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ function core.sumhexa (k)
end))
end

function core.hmac_md5(data,key)
local function get_ipad(c)
return string.char(c:byte() ~ 0x36)
end

local function get_opad(c)
return string.char(c:byte() ~ 0x5c)
end

function core.hmacmd5(data,key)
if #key>64 then
key=core.sum(key)
key=string.sub(key,1,16)
end

local b=table.pack(string.byte(key,1,#key))
local ipad_s=""
local opad_s=""
for i=1,64 do
ipad_s=ipad_s..string.char((b[i] or 0)~0x36)
opad_s=opad_s..string.char((b[i] or 0)~0x5c)
key=key:sub(1,16)
end
local ipad_s=key:gsub(".", get_ipad)..string.rep("6",64-#key)
local opad_s=key:gsub(".", get_opad)..string.rep("\\",64-#key)
local istr=core.sum(ipad_s..data)
local ostr=core.sumhexa(opad_s..istr)
return ostr
Expand Down

0 comments on commit 1999a03

Please sign in to comment.