Annotate

libs/hashes.lua @ 411:db462d4feb44

verse: trim trailing whitespace
author Kim Alvefur <zash@zash.se>
date Tue, 18 Apr 2017 16:46:26 +0200
parent 398:b4ce2e524ed8
child 414:2a5eff919f4a
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 local have_luacrypto, crypto = pcall(require, "crypto");
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
2
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
3 if have_luacrypto then
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
4 local hashes = {};
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
5
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
6 local digest = crypto.digest;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
7 local function gethash(algo)
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
8 return function (string, hex)
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
9 return digest(algo, string, not hex);
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
10 end
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
11 end
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
12
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
13 local hmac = crypto.hmac.digest;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
14 local function gethmac(algo)
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
15 return function (key, message, hex)
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
16 return hmac(algo, message, key, not hex);
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
17 end
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
18 end
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
19
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
20 local hash_algos = { "md5", "sha1", "sha256", "sha512" };
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
21
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
22 for _, hash_algo in ipairs(hash_algos) do
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
23 hashes[hash_algo] = gethash(hash_algo);
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
24 hashes["hmac_"..hash_algo] = gethmac(hash_algo);
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
25 end
129
c0be31a5ff55 libs.hashes: Pure-Lua util.hashes replacement (depends on util.sha1)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
389
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
27 return hashes;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
28 else
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
29 local sha1 = require"util.sha1".sha1;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
30 local bxor = require"bit".bxor;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
31
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
32 local s_rep = string.rep;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
33 local s_char = string.char;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
34 local s_byte = string.byte;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
35 local t_concat = table.concat;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
36
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
37 local function hmac_sha1(key, message, hexres)
398
b4ce2e524ed8 libs.hashes: Fix HMAC-SHA-1 blocksize to 64 (20 is output size)
Kim Alvefur <zash@zash.se>
parents: 389
diff changeset
38 if #key > 64 then
389
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
39 key = sha1(key);
398
b4ce2e524ed8 libs.hashes: Fix HMAC-SHA-1 blocksize to 64 (20 is output size)
Kim Alvefur <zash@zash.se>
parents: 389
diff changeset
40 elseif #key < 64 then
b4ce2e524ed8 libs.hashes: Fix HMAC-SHA-1 blocksize to 64 (20 is output size)
Kim Alvefur <zash@zash.se>
parents: 389
diff changeset
41 key = key .. s_rep("\0", 64 - #key);
389
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
42 end
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
43 local o_key_pad, i_key_pad = {}, {}
398
b4ce2e524ed8 libs.hashes: Fix HMAC-SHA-1 blocksize to 64 (20 is output size)
Kim Alvefur <zash@zash.se>
parents: 389
diff changeset
44 for i = 1, 64 do
389
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
45 local b = s_byte(key, i)
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
46 o_key_pad[i] = s_char(bxor(b, 0x5c));
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
47 i_key_pad[i] = s_char(bxor(b, 0x36));
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
48 end
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
49 o_key_pad = t_concat(o_key_pad);
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
50 i_key_pad = t_concat(i_key_pad);
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
51 return sha1(o_key_pad .. sha1(i_key_pad .. message), hexres);
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
52 end
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
53
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
54 return {
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
55 sha1 = sha1;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
56 hmac_sha1 = hmac_sha1;
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
57 };
bf3a4fcdcb76 libs.hashes: Wrap LuaCrypto with fallback to util.sha1
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
58 end