From 0ba13417109702dfe58ab29e497825366d3a6464 Mon Sep 17 00:00:00 2001 From: Dwayne Bent Date: Wed, 1 Jul 2009 20:24:47 -0400 Subject: Added HMAC utility module Produces HMAC codes using all the supported hashes (md5, sha1, sha256) --- util/hmac.lua | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 util/hmac.lua (limited to 'util') diff --git a/util/hmac.lua b/util/hmac.lua new file mode 100644 index 00000000..b3098380 --- /dev/null +++ b/util/hmac.lua @@ -0,0 +1,67 @@ +local hashes = require "util.hashes" +local xor = require "bit".bxor + +module "hmac" + +local function arraystr(array) + t = {} + for i = 1,table.getn(array) do + table.insert(t, string.char(array[i])) + end + + return table.concat(t) +end + +--[[ +key + the key to use in the hash +message + the message to hash +hash + the hash function +blocksize + the blocksize for the hash function in bytes +hex + return raw hash or hexadecimal string +--]] +function hmac(key, message, hash, blocksize, hex) + local opad = {} + local ipad = {} + + for i = 1,blocksize do + opad[i] = 0x5c + ipad[i] = 0x36 + end + + if #key > blocksize then + key = hash(key) + end + + for i = 1,#key do + ipad[i] = xor(ipad[i],key:sub(i,i):byte()) + opad[i] = xor(opad[i],key:sub(i,i):byte()) + end + + opad = arraystr(opad) + ipad = arraystr(ipad) + + if hex then + return hash(opad..hash(ipad..message), true) + else + return hash(opad..hash(ipad..message)) + end +end + +function md5(key, message, hex) + return hmac(key, message, hashes.md5, 64, hex) +end + +function sha1(key, message, hex) + return hmac(key, message, hashes.sha1, 64, hex) +end + +function sha256(key, message, hex) + return hmac(key, message, hashes.sha256, 64, hex) +end + +return _M -- cgit v1.2.3 From 9dd7b702f080143c9ef72c6ad5efd5af30a793f0 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 2 Jul 2009 21:34:45 +0500 Subject: datamanager: Fixed incorrect callback result checking --- util/datamanager.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'util') diff --git a/util/datamanager.lua b/util/datamanager.lua index 54cf1959..8e4b7828 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -62,7 +62,7 @@ end local function callback(username, host, datastore, data) for _, f in ipairs(callbacks) do username, host, datastore, data = f(username, host, datastore, data); - if not username then break; end + if username == false then break; end end return username, host, datastore, data; @@ -123,7 +123,7 @@ function store(username, host, datastore, data) end username, host, datastore, data = callback(username, host, datastore, data); - if not username then + if username == false then return true; -- Don't save this data at all end @@ -147,7 +147,7 @@ end function list_append(username, host, datastore, data) if not data then return; end - if callback and callback(username, host, datastore) then return true; end + if callback(username, host, datastore) == false then return true; end -- save the datastore local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); if not f then @@ -165,7 +165,7 @@ function list_store(username, host, datastore, data) if not data then data = {}; end - if callback and callback(username, host, datastore) then return true; end + if callback(username, host, datastore) == false then return true; end -- save the datastore local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+"); if not f then -- cgit v1.2.3 From d9b3e413479ee848bdbc1b6b5c99b0ac6ed47b0f Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 5 Jul 2009 17:06:22 +0100 Subject: util.hmac: Fix a global set --- util/hmac.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/hmac.lua b/util/hmac.lua index b3098380..05376575 100644 --- a/util/hmac.lua +++ b/util/hmac.lua @@ -4,7 +4,7 @@ local xor = require "bit".bxor module "hmac" local function arraystr(array) - t = {} + local t = {} for i = 1,table.getn(array) do table.insert(t, string.char(array[i])) end -- cgit v1.2.3 From 84294f210ad1196af663e6f8cdd5733eb6f86419 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 5 Jul 2009 17:10:42 +0100 Subject: util.hmac: Some optimisations --- util/hmac.lua | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'util') diff --git a/util/hmac.lua b/util/hmac.lua index 05376575..5f4467cf 100644 --- a/util/hmac.lua +++ b/util/hmac.lua @@ -1,15 +1,18 @@ local hashes = require "util.hashes" local xor = require "bit".bxor +local t_insert, t_concat = table.insert, table.concat; +local s_char = string.char; + module "hmac" local function arraystr(array) local t = {} - for i = 1,table.getn(array) do - table.insert(t, string.char(array[i])) + for i = 1,#array do + t_insert(t, s_char(array[i])) end - return table.concat(t) + return t_concat(t) end --[[ -- cgit v1.2.3