1 local hashes = require "util.hashes"
2 local xor = require "bit".bxor
6 local function arraystr(array)
8 for i = 1,table.getn(array) do
9 table.insert(t, string.char(array[i]))
12 return table.concat(t)
17 the key to use in the hash
23 the blocksize for the hash function in bytes
25 return raw hash or hexadecimal string
27 function hmac(key, message, hash, blocksize, hex)
31 for i = 1,blocksize do
36 if #key > blocksize then
41 ipad[i] = xor(ipad[i],key:sub(i,i):byte())
42 opad[i] = xor(opad[i],key:sub(i,i):byte())
49 return hash(opad..hash(ipad..message), true)
51 return hash(opad..hash(ipad..message))
55 function md5(key, message, hex)
56 return hmac(key, message, hashes.md5, 64, hex)
59 function sha1(key, message, hex)
60 return hmac(key, message, hashes.sha1, 64, hex)
63 function sha256(key, message, hex)
64 return hmac(key, message, hashes.sha256, 64, hex)