aboutsummaryrefslogtreecommitdiffstats
path: root/util/hmac.lua
blob: b309838050bc1047eecd486789c3329d9a1f5154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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