-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
- Loading branch information
Showing
6 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters