-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,67 @@ | ||
local _hash = require"lmbed_hash" | ||
local _util = require"eli.util" | ||
local mbed_hash = require"lmbed_hash" | ||
local util = require"eli.util" | ||
|
||
---@class HashGenerator | ||
---#DES 'HashGenerator.ctx' | ||
---@field ctx userdata | ||
---@field update fun(self: HashGenerator, data: string) | ||
---@field finish fun(self: HashGenerator, hex: boolean): string | ||
|
||
---#DES 'hash.Sha256' | ||
---@class Sha256: HashGenerator | ||
---@deprecated | ||
local Sha256 = {} | ||
Sha256.__index = Sha256 | ||
|
||
---@deprecated | ||
function Sha256:new() | ||
local sha256 = {} | ||
|
||
setmetatable(sha256, self) | ||
self.__index = self | ||
sha256.ctx = _hash.sha256init() | ||
return sha256 | ||
end | ||
|
||
---#DES 'hash.Sha256:update' | ||
--- | ||
---@param self Sha512 | ||
---@param bytes string | ||
---@deprecated | ||
function Sha256:update(bytes) self.ctx:update(bytes) end | ||
|
||
---#DES 'hash.Sha256:finish' | ||
--- | ||
---@param self Sha512 | ||
---@param hex boolean? | ||
---@return string | ||
---@deprecated | ||
function Sha256:finish(hex) return self.ctx:finish(hex) end | ||
local hash = { | ||
---#DES 'hash.sha256_sum' | ||
--- | ||
--- Calculates sha256 hash of the data | ||
--- @param data string | ||
--- @param hex boolean? (default false) - if true, returns the hash in hex format | ||
sha256_sum = mbed_hash.sha256sum, | ||
---#DES 'hash.sha512_sum' | ||
--- | ||
--- Calculates sha512 hash of the data | ||
--- @param data string | ||
--- @param hex boolean? (default false) - if true, returns the hash in hex format | ||
sha512_sum = mbed_hash.sha512sum, | ||
---#DES 'hash.sha256_init' | ||
--- | ||
--- Initializes sha256 hash | ||
--- @return HashGenerator | ||
sha256_init = mbed_hash.sha256init, | ||
---#DES 'hash.sha512_init' | ||
--- | ||
--- Initializes sha512 hash | ||
--- @return HashGenerator | ||
sha512_init = mbed_hash.sha512init, | ||
---#DES 'hash.equals' | ||
--- | ||
--- Compares two hashes | ||
--- @param hash1 string | ||
--- @param hash2 string | ||
--- @param hex boolean? (default false) - if true, compares the hashes in hex format | ||
--- @return boolean | ||
equals = mbed_hash.equals, | ||
} | ||
|
||
---#DES 'hash.Sha512' | ||
---@deprecated | ||
---@class Sha512: HashGenerator | ||
local Sha512 = {} | ||
Sha512.__index = Sha512 | ||
-- // TODO: remove deprecated functions in next version | ||
|
||
---@deprecated | ||
function Sha512:new() | ||
local sha512 = {} | ||
|
||
setmetatable(sha512, self) | ||
self.__index = self | ||
sha512.ctx = _hash.sha512init() | ||
return sha512 | ||
function hash.sha256sum(data, hex) | ||
print"Deprecation warning: use hash.sha256_sum instead" | ||
return mbed_hash.sha256sum(data, hex) | ||
end | ||
|
||
---#DES 'hash.Sha512:update' | ||
--- | ||
---@param self Sha512 | ||
---@param bytes string: test | ||
---@deprecated | ||
function Sha512:update(bytes) self.ctx:update(bytes) end | ||
function hash.sha512sum(data, hex) | ||
print"Deprecation warning: use hash.sha512_sum instead" | ||
return mbed_hash.sha512sum(data, hex) | ||
end | ||
|
||
---#DES 'hash.Sha512:finish' | ||
--- | ||
---@param self Sha512 | ||
---@param hex boolean? | ||
---@return string | ||
---@deprecated | ||
function Sha512:finish(hex) return self.ctx:finish(hex) end | ||
function hash.sha256init() | ||
print"Deprecation warning: use hash.sha256_init instead" | ||
return mbed_hash.sha256init() | ||
end | ||
|
||
---#DES 'hash.hex_equals' | ||
--- | ||
---Compares 2 hashes represented as hex strings | ||
---@param hash1 string | ||
---@param hash2 string | ||
---@return boolean | ||
---@deprecated | ||
local function _hex_equals(hash1, hash2) | ||
return _hash.equals(hash1, hash2, true) | ||
function hash.sha512init() | ||
print"Deprecation warning: use hash.sha512_init instead" | ||
return mbed_hash.sha512init() | ||
end | ||
|
||
return _util.generate_safe_functions{ | ||
Sha256 = Sha256, | ||
Sha512 = Sha512, | ||
sha256sum = _hash.sha256sum, | ||
sha512sum = _hash.sha512sum, | ||
sha256init = _hash.sha256init, | ||
sha512init = _hash.sha512init, | ||
equals = _hash.equals, | ||
hex_equals = _hex_equals, | ||
} | ||
return util.generate_safe_functions(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
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 |
---|---|---|
@@ -1,85 +1,92 @@ | ||
|
||
local _test = TEST or require 'u-test' | ||
local _ok, _eliHash = pcall(require, "eli.hash") | ||
local test = TEST or require"u-test" | ||
local ok, hash = pcall(require, "eli.hash") | ||
|
||
if not _ok then | ||
_test["eli.hash available"] = function () | ||
_test.assert(false, "eli.hash not available") | ||
if not ok then | ||
test["eli.hash available"] = function () | ||
test.assert(false, "eli.hash not available") | ||
end | ||
if not TEST then | ||
_test.summary() | ||
if not TEST then | ||
test.summary() | ||
os.exit() | ||
else | ||
return | ||
else | ||
return | ||
end | ||
end | ||
|
||
_test["eli.hash available"] = function () | ||
_test.assert(true) | ||
test["eli.hash available"] = function () | ||
test.assert(true) | ||
end | ||
|
||
_test["sha256sum"] = function () | ||
local _data = "test text\n" | ||
local _expected = "c2a4f4903509957d138e216a6d2c0d7867235c61088c02ca5cf38f2332407b00" | ||
local _result = _eliHash.sha256sum(_data, true) | ||
test["sha256sum"] = function () | ||
local data = "test text\n" | ||
local expected = "c2a4f4903509957d138e216a6d2c0d7867235c61088c02ca5cf38f2332407b00" | ||
local result = hash.sha256_sum(data, true) | ||
|
||
_test.assert(_eliHash.hex_equals(_expected, _result), "hashes do not match") | ||
test.assert(hash.hex_equals(expected, result), "hashes do not match") | ||
end | ||
|
||
_test["sha512sum"] = function () | ||
local _data = "test text\n" | ||
local _expected = "ae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
local _result = _eliHash.sha512sum(_data, true) | ||
test["sha512sum"] = function () | ||
local data = "test text\n" | ||
local expected = | ||
"ae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
local result = hash.sha512_sum(data, true) | ||
|
||
_test.assert(_eliHash.hex_equals(_expected, _result), "hashes do not match") | ||
test.assert(hash.hex_equals(expected, result), "hashes do not match") | ||
end | ||
|
||
_test["Sha256"] = function () | ||
local _sha256 = _eliHash.sha256init() | ||
local _data = "test text\n" | ||
local _expected = "c2a4f4903509957d138e216a6d2c0d7867235c61088c02ca5cf38f2332407b00" | ||
_sha256:update(_data) | ||
local _result = _sha256:finish(true) | ||
_test.assert(_eliHash.hex_equals(_expected, _result), "hashes do not match") | ||
test["Sha256"] = function () | ||
local sha256 = hash.sha256_init() | ||
local data = "test text\n" | ||
local expected = "c2a4f4903509957d138e216a6d2c0d7867235c61088c02ca5cf38f2332407b00" | ||
sha256:update(data) | ||
local result = sha256:finish(true) | ||
test.assert(hash.hex_equals(expected, result), "hashes do not match") | ||
end | ||
|
||
_test["Sha512"] = function () | ||
local _sha512 = _eliHash.sha512init() | ||
local _data = "test text\n" | ||
local _expected = "ae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
_sha512:update(_data) | ||
local _result = _sha512:finish(true) | ||
_test.assert(_eliHash.hex_equals(_expected, _result), "hashes do not match") | ||
test["Sha512"] = function () | ||
local sha512 = hash.sha512_init() | ||
local data = "test text\n" | ||
local expected = | ||
"ae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
sha512:update(data) | ||
local result = sha512:finish(true) | ||
test.assert(hash.hex_equals(expected, result), "hashes do not match") | ||
end | ||
|
||
_test["equals"] = function () | ||
local _data = "test text\n" | ||
local _data2 = "test text\n" | ||
local _data3 = "test text" | ||
local _result = _eliHash.sha256sum(_data) | ||
local _result2 = _eliHash.sha256sum(_data2) | ||
local _result3 = _eliHash.sha256sum(_data3) | ||
_test.assert(_eliHash.equals(_result, _result2), "hashes do not match") | ||
_test.assert(not _eliHash.equals(_result, _result3), "hashes match and should not") | ||
test["equals"] = function () | ||
local data = "test text\n" | ||
local data2 = "test text\n" | ||
local data3 = "test text" | ||
local result = hash.sha256_sum(data) | ||
local result2 = hash.sha256_sum(data2) | ||
local result3 = hash.sha256_sum(data3) | ||
test.assert(hash.equals(result, result2), "hashes do not match") | ||
test.assert(not hash.equals(result, result3), "hashes match and should not") | ||
end | ||
|
||
_test["hex_equals"] = function () | ||
local _hash1 = "ae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
local _hash2 = "0xae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
local _hash3 = "0Xae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
local _hash4 = string.upper(_hash2) | ||
local _hash5 = "0xAE9CD6D303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
_test.assert(_eliHash.hex_equals(_hash1, _hash2), "hashes do not match") | ||
_test.assert(_eliHash.hex_equals(_hash1, _hash3), "hashes do not match") | ||
_test.assert(_eliHash.hex_equals(_hash1, _hash4), "hashes do not match") | ||
_test.assert(_eliHash.hex_equals(_hash1, _hash5), "hashes do not match") | ||
local _hash6 = "0xAE9CD6D303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc9" | ||
local _hash7 = "ae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb6" | ||
_test.assert(not _eliHash.hex_equals(_hash1, _hash6), "hashes match and should not") | ||
_test.assert(not _eliHash.hex_equals(_hash1, _hash7), "hashes match and should not") | ||
test["hex_equals"] = function () | ||
local hash1 = | ||
"ae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
local hash2 = | ||
"0xae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
local hash3 = | ||
"0Xae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
local hash4 = string.upper(hash2) | ||
local hash5 = | ||
"0xAE9CD6D303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb5" | ||
test.assert(hash.equals(hash1, hash2, true), "hashes do not match") | ||
test.assert(hash.equals(hash1, hash3, true), "hashes do not match") | ||
test.assert(hash.equals(hash1, hash4, true), "hashes do not match") | ||
test.assert(hash.equals(hash1, hash5, true), "hashes do not match") | ||
local hash6 = | ||
"0xAE9CD6D303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc9" | ||
local hash7 = | ||
"ae9cd6d303a5836d8a6d82b468a8f968ab557243d8b16601394d29e81e6766c609fe810ee9c3988ae15b98b9cbf3a602f6905e78466b968f3a6b8201edc94cb6" | ||
test.assert(not hash.equals(hash1, hash6, true), "hashes match and should not") | ||
test.assert(not hash.equals(hash1, hash7, true), "hashes match and should not") | ||
end | ||
|
||
|
||
if not TEST then | ||
_test.summary() | ||
if not TEST then | ||
test.summary() | ||
end |