Annotate

libs/hashes.lua @ 484:5e2978489c95

libs/hashes: Support for luaossl (lua-luaossl on Debian)
author Matthew Wild <mwild1@gmail.com>
date Thu, 23 Mar 2023 18:54:13 +0000
parent 443:89526c890363
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
389
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
1
422
ff59e4a1a600 libs.hashes: Better error message when method not available
Matthew Wild <mwild1@gmail.com>
parents: 421
diff changeset
2 local function not_available(_, method_name)
ff59e4a1a600 libs.hashes: Better error message when method not available
Matthew Wild <mwild1@gmail.com>
parents: 421
diff changeset
3 error("Hash method "..method_name.." not available", 2);
414
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
4 end
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
5
422
ff59e4a1a600 libs.hashes: Better error message when method not available
Matthew Wild <mwild1@gmail.com>
parents: 421
diff changeset
6 local _M = setmetatable({}, { __index = not_available });
129
c0be31a5ff55 libs.hashes: Pure-Lua util.hashes replacement (depends on util.sha1)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
414
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
8 local function with(mod, f)
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
9 local ok, pkg = pcall(require, mod);
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
10 if ok then f(pkg); end
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
11 end
389
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
12
484
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
13 local function to_hex(data)
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
14 return (data:gsub(".", function (c)
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
15 return ("%02x"):format(c:byte());
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
16 end));
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
17 end
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
18
443
89526c890363 libs.hashes: Add back pure Lua sha1 implementation
Kim Alvefur <zash@zash.se>
parents: 422
diff changeset
19 with("util.sha1", function (sha1)
89526c890363 libs.hashes: Add back pure Lua sha1 implementation
Kim Alvefur <zash@zash.se>
parents: 422
diff changeset
20 _M.sha1 = sha1.sha1;
89526c890363 libs.hashes: Add back pure Lua sha1 implementation
Kim Alvefur <zash@zash.se>
parents: 422
diff changeset
21 end);
89526c890363 libs.hashes: Add back pure Lua sha1 implementation
Kim Alvefur <zash@zash.se>
parents: 422
diff changeset
22
414
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
23 with("bgcrypto.md5", function (md5)
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
24 _M.md5 = md5.digest;
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
25 _M.hmac_md5 = md5.hmac.digest;
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
26 end);
389
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
27
414
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
28 with("bgcrypto.sha1", function (sha1)
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
29 _M.sha1 = sha1.digest;
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
30 _M.hmac_sha1 = sha1.hmac.digest;
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
31 _M.scram_Hi_sha1 = function (p, s, i) return sha1.pbkdf2(p, s, i, 20); end;
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
32 end);
389
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
33
414
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
34 with("bgcrypto.sha256", function (sha256)
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
35 _M.sha256 = sha256.digest;
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
36 _M.hmac_sha256 = sha256.hmac.digest;
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
37 end);
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
38
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
39 with("bgcrypto.sha512", function (sha512)
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
40 _M.sha512 = sha512.digest;
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
41 _M.hmac_sha512 = sha512.hmac.digest;
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
42 end);
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
43
421
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
44 with("sha1", function (sha1)
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
45 _M.sha1 = function (data, hex)
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
46 if hex then
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
47 return sha1.sha1(data);
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
48 else
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
49 return (sha1.binary(data));
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
50 end
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
51 end;
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
52 end);
f35cfdff31b6 libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents: 414
diff changeset
53
484
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
54 with("openssl.digest", function (digest)
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
55 local function make_digest_func(digest_name)
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
56 return function (data, hex)
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
57 local d = digest.new(digest_name):final(data);
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
58 if hex then
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
59 return to_hex(d);
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
60 end
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
61 return d;
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
62 end;
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
63 end
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
64 _M.sha1 = make_digest_func("sha1");
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
65 end);
5e2978489c95 libs/hashes: Support for luaossl (lua-luaossl on Debian)
Matthew Wild <mwild1@gmail.com>
parents: 443
diff changeset
66
414
2a5eff919f4a libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents: 398
diff changeset
67 return _M;