Skip to content

Commit aea6857

Browse files
committed
feat(hash): add support hmac
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
1 parent f17a658 commit aea6857

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ install(
192192
DESTINATION ${LUA_INSTALL_PREFIX}/eco/hash
193193
)
194194

195+
install(
196+
FILES hmac.lua
197+
DESTINATION ${LUA_INSTALL_PREFIX}/eco/hash
198+
)
199+
195200
install(
196201
FILES time.lua sys.lua file.lua dns.lua socket.lua packet.lua mqtt.lua
197202
websocket.lua sync.lua channel.lua nl.lua genl.lua ip.lua nl80211.lua

examples/hash/hmac.lua

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env eco
2+
3+
local sha256 = require 'eco.hash.sha256'
4+
local hmac = require 'eco.hash.hmac'
5+
local hex = require 'eco.encoding.hex'
6+
7+
-- also, you can use other hash modules, such as sha1, md5.
8+
local ctx = hmac.new(sha256, 'key')
9+
10+
ctx:update('12')
11+
ctx:update('34')
12+
13+
local hash = ctx:final()
14+
print(hex.encode(hash))
15+
16+
hash = hmac.sum(sha256, 'key', '1234')
17+
print(hex.encode(hash))

hmac.lua

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- SPDX-License-Identifier: MIT
2+
-- Author: Jianhui Zhao <zhaojh329@gmail.com>
3+
4+
local M = {}
5+
6+
local blocksize = 64
7+
8+
local methods = {}
9+
10+
function methods:update(data)
11+
assert(type(data) == 'string', 'expecting data to be a string')
12+
13+
self.ctx:update(data)
14+
end
15+
16+
function methods:final()
17+
return self.hash.sum(self.key .. self.ctx:final())
18+
end
19+
20+
local metatable = {
21+
__index = methods
22+
}
23+
24+
function M.new(hash, key)
25+
local mtname = hash and hash.mtname
26+
27+
assert(mtname == 'eco{md5}'
28+
or mtname == 'eco{sha1}'
29+
or mtname == 'eco{sha256}',
30+
'expecting hash to be a hash module')
31+
32+
assert(type(key) == 'string', 'expecting key to be a string')
33+
34+
if #key > blocksize then
35+
key = hash.sum(key)
36+
end
37+
38+
local key_xord_with_0x36 = key:gsub('.', function(c)
39+
return string.char(c:byte() ~ 0x36)
40+
end) .. string.rep(string.char(0x36), blocksize - #key)
41+
42+
local key_xord_with_0x5c = key:gsub('.', function(c)
43+
return string.char(c:byte() ~ 0x5c)
44+
end) .. string.rep(string.char(0x5c), blocksize - #key)
45+
46+
local ctx = hash.new()
47+
48+
ctx:update(key_xord_with_0x36)
49+
50+
return setmetatable({
51+
ctx = ctx,
52+
hash = hash,
53+
key = key_xord_with_0x5c
54+
}, metatable)
55+
end
56+
57+
function M.sum(hash, key, data)
58+
local ctx = M.new(hash, key)
59+
60+
ctx:update(data)
61+
62+
return ctx:final()
63+
end
64+
65+
return M

md5.c

+3
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ int luaopen_eco_hash_md5(lua_State *L)
330330
{
331331
lua_newtable(L);
332332

333+
lua_pushstring(L, ECO_MD5_MT);
334+
lua_setfield(L, -2, "mtname");
335+
333336
lua_pushcfunction(L, lua_md5_sum);
334337
lua_setfield(L, -2, "sum");
335338

sha1.c

+3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ int luaopen_eco_hash_sha1(lua_State *L)
272272
{
273273
lua_newtable(L);
274274

275+
lua_pushstring(L, ECO_SHA1_MT);
276+
lua_setfield(L, -2, "mtname");
277+
275278
lua_pushcfunction(L, lua_sha1_sum);
276279
lua_setfield(L, -2, "sum");
277280

sha256.c

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ int luaopen_eco_hash_sha256(lua_State *L)
220220
{
221221
lua_newtable(L);
222222

223+
lua_pushstring(L, ECO_SHA256_MT);
224+
lua_setfield(L, -2, "mtname");
225+
223226
lua_pushcfunction(L, lua_sha256_sum);
224227
lua_setfield(L, -2, "sum");
225228

0 commit comments

Comments
 (0)