1 local hashes = require "util.hashes"
2 local xor = require "bit".bxor
4 local t_insert, t_concat = table.insert, table.concat;
5 local s_char = string.char;
9 local function arraystr(array)
12 t_insert(t, s_char(array[i]))
20 the key to use in the hash
26 the blocksize for the hash function in bytes
28 return raw hash or hexadecimal string
30 function hmac(key, message, hash, blocksize, hex)
34 for i = 1,blocksize do
39 if #key > blocksize then
44 ipad[i] = xor(ipad[i],key:sub(i,i):byte())
45 opad[i] = xor(opad[i],key:sub(i,i):byte())
52 return hash(opad..hash(ipad..message), true)
54 return hash(opad..hash(ipad..message))
58 function md5(key, message, hex)
59 return hmac(key, message, hashes.md5, 64, hex)
62 function sha1(key, message, hex)
63 return hmac(key, message, hashes.sha1, 64, hex)
66 function sha256(key, message, hex)
67 return hmac(key, message, hashes.sha256, 64, hex)