Skip to content

Commit

Permalink
feat(hash): add support hmac
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Dec 20, 2024
1 parent f17a658 commit aea6857
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ install(
DESTINATION ${LUA_INSTALL_PREFIX}/eco/hash
)

install(
FILES hmac.lua
DESTINATION ${LUA_INSTALL_PREFIX}/eco/hash
)

install(
FILES time.lua sys.lua file.lua dns.lua socket.lua packet.lua mqtt.lua
websocket.lua sync.lua channel.lua nl.lua genl.lua ip.lua nl80211.lua
Expand Down
17 changes: 17 additions & 0 deletions examples/hash/hmac.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env eco

local sha256 = require 'eco.hash.sha256'
local hmac = require 'eco.hash.hmac'
local hex = require 'eco.encoding.hex'

-- also, you can use other hash modules, such as sha1, md5.
local ctx = hmac.new(sha256, 'key')

ctx:update('12')
ctx:update('34')

local hash = ctx:final()
print(hex.encode(hash))

hash = hmac.sum(sha256, 'key', '1234')
print(hex.encode(hash))
65 changes: 65 additions & 0 deletions hmac.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- SPDX-License-Identifier: MIT
-- Author: Jianhui Zhao <zhaojh329@gmail.com>

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
3 changes: 3 additions & 0 deletions md5.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ int luaopen_eco_hash_md5(lua_State *L)
{
lua_newtable(L);

lua_pushstring(L, ECO_MD5_MT);
lua_setfield(L, -2, "mtname");

lua_pushcfunction(L, lua_md5_sum);
lua_setfield(L, -2, "sum");

Expand Down
3 changes: 3 additions & 0 deletions sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ int luaopen_eco_hash_sha1(lua_State *L)
{
lua_newtable(L);

lua_pushstring(L, ECO_SHA1_MT);
lua_setfield(L, -2, "mtname");

lua_pushcfunction(L, lua_sha1_sum);
lua_setfield(L, -2, "sum");

Expand Down
3 changes: 3 additions & 0 deletions sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ int luaopen_eco_hash_sha256(lua_State *L)
{
lua_newtable(L);

lua_pushstring(L, ECO_SHA256_MT);
lua_setfield(L, -2, "mtname");

lua_pushcfunction(L, lua_sha256_sum);
lua_setfield(L, -2, "sum");

Expand Down

0 comments on commit aea6857

Please # to comment.