Merge pull request #84 from YunoHost/caching_for_hash

[enh] uses caching for hash to avoid heavy recalculation and process spawning
This commit is contained in:
Laurent Peuch 2017-05-23 21:40:30 +02:00 committed by GitHub
commit c1a388ccf0

View file

@ -72,6 +72,9 @@ end
-- Hash a string using hmac_sha512, return a hexa string -- Hash a string using hmac_sha512, return a hexa string
function hmac_sha512(key, message) function hmac_sha512(key, message)
local cache_key = key..":"..message
if not cache:get(cache_key) then
-- lua ecosystem is a disaster and it was not possible to find a good -- lua ecosystem is a disaster and it was not possible to find a good
-- easily multiplatform integrable code for this -- easily multiplatform integrable code for this
-- Python has this buildin, so we call it directly -- Python has this buildin, so we call it directly
@ -88,7 +91,12 @@ function hmac_sha512(key, message)
-- so we need to remove the "(stdin)= " at the beginning -- so we need to remove the "(stdin)= " at the beginning
local hash = pipe:read():sub(string.len("(stdin)= ") + 1) local hash = pipe:read():sub(string.len("(stdin)= ") + 1)
pipe:close() pipe:close()
cache:set(cache_key, hash, conf["session_timeout"])
return hash return hash
else
return cache:get(cache_key)
end
end end