| -rw-r--r-- | filters/email-gravatar.lua | 17 | ||||
| -rw-r--r-- | filters/email-libravatar.lua | 17 | ||||
| -rw-r--r-- | filters/file-authentication.lua | 31 | ||||
| -rw-r--r-- | filters/gentoo-ldap-authentication.lua | 31 | ||||
| -rw-r--r-- | filters/simple-authentication.lua | 31 |
5 files changed, 44 insertions, 83 deletions
diff --git a/filters/email-gravatar.lua b/filters/email-gravatar.lua index c39b490..52cf426 100644 --- a/filters/email-gravatar.lua +++ b/filters/email-gravatar.lua @@ -3,24 +3,15 @@ -- prefix in filters. It is much faster than the corresponding python script. -- -- Requirements: --- luaossl --- <http://25thandclement.com/~william/projects/luaossl.html> +-- luacrypto >= 0.3 +-- <http://mkottman.github.io/luacrypto/> -- -local digest = require("openssl.digest") - -function md5_hex(input) - local b = digest.new("md5"):final(input) - local x = "" - for i = 1, #b do - x = x .. string.format("%.2x", string.byte(b, i)) - end - return x -end +local crypto = require("crypto") function filter_open(email, page) buffer = "" - md5 = md5_hex(email:sub(2, -2):lower()) + md5 = crypto.digest("md5", email:sub(2, -2):lower()) end function filter_close() diff --git a/filters/email-libravatar.lua b/filters/email-libravatar.lua index 7336baf..b0e2447 100644 --- a/filters/email-libravatar.lua +++ b/filters/email-libravatar.lua @@ -3,24 +3,15 @@ -- prefix in filters. -- -- Requirements: --- luaossl --- <http://25thandclement.com/~william/projects/luaossl.html> +-- luacrypto >= 0.3 +-- <http://mkottman.github.io/luacrypto/> -- -local digest = require("openssl.digest") - -function md5_hex(input) - local b = digest.new("md5"):final(input) - local x = "" - for i = 1, #b do - x = x .. string.format("%.2x", string.byte(b, i)) - end - return x -end +local crypto = require("crypto") function filter_open(email, page) buffer = "" - md5 = md5_hex(email:sub(2, -2):lower()) + md5 = crypto.digest("md5", email:sub(2, -2):lower()) end function filter_close() diff --git a/filters/file-authentication.lua b/filters/file-authentication.lua index 0248804..6ee1e19 100644 --- a/filters/file-authentication.lua +++ b/filters/file-authentication.lua @@ -1,15 +1,15 @@ -- This script may be used with the auth-filter. -- -- Requirements: --- luaossl --- <http://25thandclement.com/~william/projects/luaossl.html> +-- luacrypto >= 0.3 +-- <http://mkottman.github.io/luacrypto/> -- luaposix -- <https://github.com/luaposix/luaposix> -- local sysstat = require("posix.sys.stat") local unistd = require("posix.unistd") -local rand = require("openssl.rand") -local hmac = require("openssl.hmac") +local crypto = require("crypto") + -- This file should contain a series of lines in the form of: -- username1:hash1 @@ -225,13 +225,6 @@ function get_cookie(cookies, name) return url_decode(string.match(cookies, ";" .. name .. "=(.-);")) end -function tohex(b) - local x = "" - for i = 1, #b do - x = x .. string.format("%.2x", string.byte(b, i)) - end - return x -end -- -- @@ -249,12 +242,12 @@ function get_secret() local secret_file = io.open(secret_filename, "r") if secret_file == nil then local old_umask = sysstat.umask(63) - local temporary_filename = secret_filename .. ".tmp." .. tohex(rand.bytes(16)) + local temporary_filename = secret_filename .. ".tmp." .. crypto.hex(crypto.rand.bytes(16)) local temporary_file = io.open(temporary_filename, "w") if temporary_file == nil then os.exit(177) end - temporary_file:write(tohex(rand.bytes(32))) + temporary_file:write(crypto.hex(crypto.rand.bytes(32))) temporary_file:close() unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same. unistd.unlink(temporary_filename) @@ -279,7 +272,7 @@ function validate_value(expected_field, cookie) local field = "" local expiration = 0 local salt = "" - local chmac = "" + local hmac = "" if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then return nil @@ -298,19 +291,19 @@ function validate_value(expected_field, cookie) elseif i == 3 then salt = component elseif i == 4 then - chmac = component + hmac = component else break end i = i + 1 end - if chmac == nil or chmac:len() == 0 then + if hmac == nil or hmac:len() == 0 then return nil end -- Lua hashes strings, so these comparisons are time invariant. - if chmac ~= tohex(hmac.new(get_secret(), "sha256"):final(field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt)) then + if hmac ~= crypto.hmac.digest("sha256", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, get_secret()) then return nil end @@ -331,11 +324,11 @@ function secure_value(field, value, expiration) end local authstr = "" - local salt = tohex(rand.bytes(16)) + local salt = crypto.hex(crypto.rand.bytes(16)) value = url_encode(value) field = url_encode(field) authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt - authstr = authstr .. "|" .. tohex(hmac.new(get_secret(), "sha256"):final(authstr)) + authstr = authstr .. "|" .. crypto.hmac.digest("sha256", authstr, get_secret()) return authstr end diff --git a/filters/gentoo-ldap-authentication.lua b/filters/gentoo-ldap-authentication.lua index 673c88d..b4d98c2 100644 --- a/filters/gentoo-ldap-authentication.lua +++ b/filters/gentoo-ldap-authentication.lua @@ -1,8 +1,8 @@ -- This script may be used with the auth-filter. Be sure to configure it as you wish. -- -- Requirements: --- luaossl --- <http://25thandclement.com/~william/projects/luaossl.html> +-- luacrypto >= 0.3 +-- <http://mkottman.github.io/luacrypto/> -- lualdap >= 1.2 -- <https://git.zx2c4.com/lualdap/about/> -- luaposix @@ -10,9 +10,9 @@ -- local sysstat = require("posix.sys.stat") local unistd = require("posix.unistd") +local crypto = require("crypto") local lualdap = require("lualdap") -local rand = require("openssl.rand") -local hmac = require("openssl.hmac") + -- -- @@ -226,13 +226,6 @@ function get_cookie(cookies, name) return string.match(cookies, ";" .. name .. "=(.-);") end -function tohex(b) - local x = "" - for i = 1, #b do - x = x .. string.format("%.2x", string.byte(b, i)) - end - return x -end -- -- @@ -250,12 +243,12 @@ function get_secret() local secret_file = io.open(secret_filename, "r") if secret_file == nil then local old_umask = sysstat.umask(63) - local temporary_filename = secret_filename .. ".tmp." .. tohex(rand.bytes(16)) + local temporary_filename = secret_filename .. ".tmp." .. crypto.hex(crypto.rand.bytes(16)) local temporary_file = io.open(temporary_filename, "w") if temporary_file == nil then os.exit(177) end - temporary_file:write(tohex(rand.bytes(32))) + temporary_file:write(crypto.hex(crypto.rand.bytes(32))) temporary_file:close() unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same. unistd.unlink(temporary_filename) @@ -280,7 +273,7 @@ function validate_value(expected_field, cookie) local field = "" local expiration = 0 local salt = "" - local chmac = "" + local hmac = "" if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then return nil @@ -299,19 +292,19 @@ function validate_value(expected_field, cookie) elseif i == 3 then salt = component elseif i == 4 then - chmac = component + hmac = component else break end i = i + 1 end - if chmac == nil or chmac:len() == 0 then + if hmac == nil or hmac:len() == 0 then return nil end -- Lua hashes strings, so these comparisons are time invariant. - if chmac ~= tohex(hmac.new(get_secret(), "sha256"):final(field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt)) then + if hmac ~= crypto.hmac.digest("sha256", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, get_secret()) then return nil end @@ -332,11 +325,11 @@ function secure_value(field, value, expiration) end local authstr = "" - local salt = tohex(rand.bytes(16)) + local salt = crypto.hex(crypto.rand.bytes(16)) value = url_encode(value) field = url_encode(field) authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt - authstr = authstr .. "|" .. tohex(hmac.new(get_secret(), "sha256"):final(authstr)) + authstr = authstr .. "|" .. crypto.hmac.digest("sha256", authstr, get_secret()) return authstr end diff --git a/filters/simple-authentication.lua b/filters/simple-authentication.lua index 23d3457..77d1fd0 100644 --- a/filters/simple-authentication.lua +++ b/filters/simple-authentication.lua @@ -1,15 +1,15 @@ -- This script may be used with the auth-filter. Be sure to configure it as you wish. -- -- Requirements: --- luaossl --- <http://25thandclement.com/~william/projects/luaossl.html> +-- luacrypto >= 0.3 +-- <http://mkottman.github.io/luacrypto/> -- luaposix -- <https://github.com/luaposix/luaposix> -- local sysstat = require("posix.sys.stat") local unistd = require("posix.unistd") -local rand = require("openssl.rand") -local hmac = require("openssl.hmac") +local crypto = require("crypto") + -- -- @@ -180,13 +180,6 @@ function get_cookie(cookies, name) return url_decode(string.match(cookies, ";" .. name .. "=(.-);")) end -function tohex(b) - local x = "" - for i = 1, #b do - x = x .. string.format("%.2x", string.byte(b, i)) - end - return x -end -- -- @@ -204,12 +197,12 @@ function get_secret() local secret_file = io.open(secret_filename, "r") if secret_file == nil then local old_umask = sysstat.umask(63) - local temporary_filename = secret_filename .. ".tmp." .. tohex(rand.bytes(16)) + local temporary_filename = secret_filename .. ".tmp." .. crypto.hex(crypto.rand.bytes(16)) local temporary_file = io.open(temporary_filename, "w") if temporary_file == nil then os.exit(177) end - temporary_file:write(tohex(rand.bytes(32))) + temporary_file:write(crypto.hex(crypto.rand.bytes(32))) temporary_file:close() unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same. unistd.unlink(temporary_filename) @@ -234,7 +227,7 @@ function validate_value(expected_field, cookie) local field = "" local expiration = 0 local salt = "" - local chmac = "" + local hmac = "" if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then return nil @@ -253,19 +246,19 @@ function validate_value(expected_field, cookie) elseif i == 3 then salt = component elseif i == 4 then - chmac = component + hmac = component else break end i = i + 1 end - if chmac == nil or chmac:len() == 0 then + if hmac == nil or hmac:len() == 0 then return nil end -- Lua hashes strings, so these comparisons are time invariant. - if chmac ~= tohex(hmac.new(get_secret(), "sha256"):final(field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt)) then + if hmac ~= crypto.hmac.digest("sha256", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, get_secret()) then return nil end @@ -286,11 +279,11 @@ function secure_value(field, value, expiration) end local authstr = "" - local salt = tohex(rand.bytes(16)) + local salt = crypto.hex(crypto.rand.bytes(16)) value = url_encode(value) field = url_encode(field) authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt - authstr = authstr .. "|" .. tohex(hmac.new(get_secret(), "sha256"):final(authstr)) + authstr = authstr .. "|" .. crypto.hmac.digest("sha256", authstr, get_secret()) return authstr end |