Changeset

414:2a5eff919f4a

libs.hashes: Switch to moteus bgcrypto suite Because LuaCrypto seems to be unmaintained and bgcrypto has PBKDF2 https://luarocks.org/modules/moteus/bgcrypto-lmd5 https://luarocks.org/modules/moteus/bgcrypto-sha https://luarocks.org/modules/moteus/bgcrypto-hmac https://luarocks.org/modules/moteus/bgcrypto-pbkdf2
author Kim Alvefur <zash@zash.se>
date Sun, 20 May 2018 00:51:58 +0200
parents 413:41c67bf8c010
children 415:37674f8ce263
files libs/hashes.lua
diffstat 1 files changed, 39 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/libs/hashes.lua	Sat Mar 31 15:45:14 2018 +0200
+++ b/libs/hashes.lua	Sun May 20 00:51:58 2018 +0200
@@ -1,58 +1,47 @@
-local have_luacrypto, crypto = pcall(require, "crypto");
-
-if have_luacrypto then
-	local hashes = {};
 
-	local digest = crypto.digest;
-	local function gethash(algo)
-		return function (string, hex)
-			return digest(algo, string, not hex);
-		end
-	end
+local function not_available()
+	error("not available", 2);
+end
+
+local _M = {
+	md5 = not_available;
+	hmac_md5 = not_available;
 
-	local hmac = crypto.hmac.digest;
-	local function gethmac(algo)
-		return function (key, message, hex)
-			return hmac(algo, message, key, not hex);
-		end
-	end
+	sha1 = not_available;
+	hmac_sha1 = not_available;
+	scram_Hi_sha1 = not_available;
 
-	local hash_algos = { "md5", "sha1", "sha256", "sha512" };
+	sha256 = not_available;
+	hmac_sha256 = not_available;
 
-	for _, hash_algo in ipairs(hash_algos) do
-		hashes[hash_algo] = gethash(hash_algo);
-		hashes["hmac_"..hash_algo] = gethmac(hash_algo);
-	end
+	sha512 = not_available;
+	hmac_sha512 = not_available;
+};
 
-	return hashes;
-else
-	local sha1 = require"util.sha1".sha1;
-	local bxor = require"bit".bxor;
+local function with(mod, f)
+	local ok, pkg = pcall(require, mod);
+	if ok then f(pkg); end
+end
 
-	local s_rep = string.rep;
-	local s_char = string.char;
-	local s_byte = string.byte;
-	local t_concat = table.concat;
+with("bgcrypto.md5", function (md5)
+	_M.md5 = md5.digest;
+	_M.hmac_md5 = md5.hmac.digest;
+end);
 
-	local function hmac_sha1(key, message, hexres)
-		if #key > 64 then
-			key = sha1(key);
-		elseif #key < 64 then
-			key = key .. s_rep("\0", 64 - #key);
-		end
-		local o_key_pad, i_key_pad = {}, {}
-		for i = 1, 64 do
-			local b = s_byte(key, i)
-			o_key_pad[i] = s_char(bxor(b, 0x5c));
-			i_key_pad[i] = s_char(bxor(b, 0x36));
-		end
-		o_key_pad = t_concat(o_key_pad);
-		i_key_pad = t_concat(i_key_pad);
-		return sha1(o_key_pad .. sha1(i_key_pad .. message), hexres);
-	end
+with("bgcrypto.sha1", function (sha1)
+	_M.sha1 = sha1.digest;
+	_M.hmac_sha1 = sha1.hmac.digest;
+	_M.scram_Hi_sha1 = function (p, s, i) return sha1.pbkdf2(p, s, i, 20); end;
+end);
 
-	return {
-		sha1 = sha1;
-		hmac_sha1 = hmac_sha1;
-	};
-end
+with("bgcrypto.sha256", function (sha256)
+	_M.sha256 = sha256.digest;
+	_M.hmac_sha256 = sha256.hmac.digest;
+end);
+
+with("bgcrypto.sha512", function (sha512)
+	_M.sha512 = sha512.digest;
+	_M.hmac_sha512 = sha512.hmac.digest;
+end);
+
+return _M;