Software /
code /
verse
Annotate
libs/hashes.lua @ 426:9813b17ee096
squishy: Include util.x509, required by net.http in Prosody 0.10+
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 17 Jun 2019 12:32:00 +0100 |
parent | 422:ff59e4a1a600 |
child | 443:89526c890363 |
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 |
414
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
13 with("bgcrypto.md5", function (md5) |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
14 _M.md5 = md5.digest; |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
15 _M.hmac_md5 = md5.hmac.digest; |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
16 end); |
389
bf3a4fcdcb76
libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents:
129
diff
changeset
|
17 |
414
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
18 with("bgcrypto.sha1", function (sha1) |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
19 _M.sha1 = sha1.digest; |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
20 _M.hmac_sha1 = sha1.hmac.digest; |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
21 _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
|
22 end); |
389
bf3a4fcdcb76
libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents:
129
diff
changeset
|
23 |
414
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
24 with("bgcrypto.sha256", function (sha256) |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
25 _M.sha256 = sha256.digest; |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
26 _M.hmac_sha256 = sha256.hmac.digest; |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
27 end); |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
28 |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
29 with("bgcrypto.sha512", function (sha512) |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
30 _M.sha512 = sha512.digest; |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
31 _M.hmac_sha512 = sha512.hmac.digest; |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
32 end); |
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
33 |
421
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
34 with("sha1", function (sha1) |
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
35 _M.sha1 = function (data, hex) |
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
36 if hex then |
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
37 return sha1.sha1(data); |
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
38 else |
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
39 return (sha1.binary(data)); |
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
40 end |
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
41 end; |
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
42 end); |
f35cfdff31b6
libs.hashes: Add support for sha1 lib from luarocks
Matthew Wild <mwild1@gmail.com>
parents:
414
diff
changeset
|
43 |
414
2a5eff919f4a
libs.hashes: Switch to moteus bgcrypto suite
Kim Alvefur <zash@zash.se>
parents:
398
diff
changeset
|
44 return _M; |