-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhmac.lua
65 lines (45 loc) · 1.35 KB
/
hmac.lua
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
-- SPDX-License-Identifier: MIT
-- Author: Jianhui Zhao <[email protected]>
local M = {}
local blocksize = 64
local methods = {}
function methods:update(data)
assert(type(data) == 'string', 'expecting data to be a string')
self.ctx:update(data)
end
function methods:final()
return self.hash.sum(self.key .. self.ctx:final())
end
local metatable = {
__index = methods
}
function M.new(hash, key)
local mtname = hash and hash.mtname
assert(mtname == 'eco{md5}'
or mtname == 'eco{sha1}'
or mtname == 'eco{sha256}',
'expecting hash to be a hash module')
assert(type(key) == 'string', 'expecting key to be a string')
if #key > blocksize then
key = hash.sum(key)
end
local key_xord_with_0x36 = key:gsub('.', function(c)
return string.char(c:byte() ~ 0x36)
end) .. string.rep(string.char(0x36), blocksize - #key)
local key_xord_with_0x5c = key:gsub('.', function(c)
return string.char(c:byte() ~ 0x5c)
end) .. string.rep(string.char(0x5c), blocksize - #key)
local ctx = hash.new()
ctx:update(key_xord_with_0x36)
return setmetatable({
ctx = ctx,
hash = hash,
key = key_xord_with_0x5c
}, metatable)
end
function M.sum(hash, key, data)
local ctx = M.new(hash, key)
ctx:update(data)
return ctx:final()
end
return M