libs/hashes: Support for luaossl (lua-luaossl on Debian)

Thu, 23 Mar 2023 18:54:13 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Mar 2023 18:54:13 +0000
changeset 484
5e2978489c95
parent 483
33d566513eb0
child 485
c9a144591649

libs/hashes: Support for luaossl (lua-luaossl on Debian)

libs/hashes.lua file | annotate | diff | comparison | revisions
--- a/libs/hashes.lua	Fri Mar 17 17:09:00 2023 +0000
+++ b/libs/hashes.lua	Thu Mar 23 18:54:13 2023 +0000
@@ -10,6 +10,12 @@
 	if ok then f(pkg); end
 end
 
+local function to_hex(data)
+	return (data:gsub(".", function (c)
+		return ("%02x"):format(c:byte());
+	end));
+end
+
 with("util.sha1", function (sha1)
 	_M.sha1 = sha1.sha1;
 end);
@@ -45,4 +51,17 @@
 	end;
 end);
 
+with("openssl.digest", function (digest)
+	local function make_digest_func(digest_name)
+		return function (data, hex)
+			local d = digest.new(digest_name):final(data);
+			if hex then
+				return to_hex(d);
+			end
+			return d;
+		end;
+	end
+	_M.sha1 = make_digest_func("sha1");
+end);
+
 return _M;

mercurial