libs/hashes.lua

changeset 484
5e2978489c95
parent 443
89526c890363
equal deleted inserted replaced
483:33d566513eb0 484:5e2978489c95
6 local _M = setmetatable({}, { __index = not_available }); 6 local _M = setmetatable({}, { __index = not_available });
7 7
8 local function with(mod, f) 8 local function with(mod, f)
9 local ok, pkg = pcall(require, mod); 9 local ok, pkg = pcall(require, mod);
10 if ok then f(pkg); end 10 if ok then f(pkg); end
11 end
12
13 local function to_hex(data)
14 return (data:gsub(".", function (c)
15 return ("%02x"):format(c:byte());
16 end));
11 end 17 end
12 18
13 with("util.sha1", function (sha1) 19 with("util.sha1", function (sha1)
14 _M.sha1 = sha1.sha1; 20 _M.sha1 = sha1.sha1;
15 end); 21 end);
43 return (sha1.binary(data)); 49 return (sha1.binary(data));
44 end 50 end
45 end; 51 end;
46 end); 52 end);
47 53
54 with("openssl.digest", function (digest)
55 local function make_digest_func(digest_name)
56 return function (data, hex)
57 local d = digest.new(digest_name):final(data);
58 if hex then
59 return to_hex(d);
60 end
61 return d;
62 end;
63 end
64 _M.sha1 = make_digest_func("sha1");
65 end);
66
48 return _M; 67 return _M;

mercurial