2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local hashes = require "util.hashes"
10 local xor = require "bit".bxor
12 local t_insert, t_concat = table.insert, table.concat;
13 local s_char = string.char;
17 local function arraystr(array)
20 t_insert(t, s_char(array[i]))
28 the key to use in the hash
34 the blocksize for the hash function in bytes
36 return raw hash or hexadecimal string
38 function hmac(key, message, hash, blocksize, hex)
42 for i = 1,blocksize do
47 if #key > blocksize then
52 ipad[i] = xor(ipad[i],key:sub(i,i):byte())
53 opad[i] = xor(opad[i],key:sub(i,i):byte())
60 return hash(opad..hash(ipad..message), true)
62 return hash(opad..hash(ipad..message))
66 function md5(key, message, hex)
67 return hmac(key, message, hashes.md5, 64, hex)
70 function sha1(key, message, hex)
71 return hmac(key, message, hashes.sha1, 64, hex)
74 function sha256(key, message, hex)
75 return hmac(key, message, hashes.sha256, 64, hex)