From f5740e00b17d125e0be02510a93b7db6aea59b5a Mon Sep 17 00:00:00 2001 From: nbwk1988 Date: Mon, 26 Oct 2015 16:12:10 +0800 Subject: [PATCH 1/2] add hmac_md5 add hmac_md5 --- lualib/md5.lua | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lualib/md5.lua b/lualib/md5.lua index 26f5cfa6..ceaddb38 100644 --- a/lualib/md5.lua +++ b/lualib/md5.lua @@ -15,4 +15,22 @@ function core.sumhexa (k) end)) end -return core \ No newline at end of file +function core.hmac_md5(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) + end + local istr=core.sum(ipad_s..data) + local ostr=core.sumhexa(opad_s..istr) + return ostr +end + +return core From 1999a03ff7f913fe97eb58c3a7c5baa17f73da49 Mon Sep 17 00:00:00 2001 From: nbwk1988 Date: Tue, 27 Oct 2015 10:44:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96hmac?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/md5.lua | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lualib/md5.lua b/lualib/md5.lua index ceaddb38..90e28739 100644 --- a/lualib/md5.lua +++ b/lualib/md5.lua @@ -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