Skip to content

Commit 5c7ca2a

Browse files
bungleleafo
authored andcommitted
pgmoon.crypto hmac_sha256 to support resty.openssl
1 parent 305cafa commit 5c7ca2a

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

pgmoon/crypto.lua

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,28 @@ else
2929
end
3030
end
3131
local hmac_sha256
32-
hmac_sha256 = function(key, str)
33-
local openssl_hmac = require("openssl.hmac")
34-
local hmac = assert(openssl_hmac.new(key, "sha256"))
35-
hmac:update(str)
36-
return assert(hmac:final())
32+
if pcall(function()
33+
return require("openssl.hmac")
34+
end) then
35+
hmac_sha256 = function(key, str)
36+
local openssl_hmac = require("openssl.hmac")
37+
local hmac = assert(openssl_hmac.new(key, "sha256"))
38+
hmac:update(str)
39+
return assert(hmac:final())
40+
end
41+
elseif pcall(function()
42+
return require("resty.openssl.hmac")
43+
end) then
44+
hmac_sha256 = function(key, str)
45+
local openssl_hmac = require("resty.openssl.hmac")
46+
local hmac = assert(openssl_hmac.new(key, "sha256"))
47+
hmac:update(str)
48+
return assert(hmac:final())
49+
end
50+
else
51+
hmac_sha256 = function()
52+
return error("Either luaossl or resty.openssl is required to calculate hmac sha256 digest")
53+
end
3754
end
3855
local digest_sha256
3956
digest_sha256 = function(str)

pgmoon/crypto.moon

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@ elseif pcall -> require "crypto"
1313
else
1414
-> error "Either luaossl (recommended) or LuaCrypto is required to calculate md5"
1515

16-
hmac_sha256 = (key, str) ->
17-
openssl_hmac = require("openssl.hmac")
18-
hmac = assert openssl_hmac.new(key, "sha256")
1916

20-
hmac\update str
21-
assert hmac\final!
17+
hmac_sha256 = if pcall -> require "openssl.hmac"
18+
(key, str) ->
19+
openssl_hmac = require("openssl.hmac")
20+
hmac = assert openssl_hmac.new(key, "sha256")
21+
22+
hmac\update str
23+
assert hmac\final!
24+
elseif pcall -> require "resty.openssl.hmac"
25+
(key, str) ->
26+
openssl_hmac = require("resty.openssl.hmac")
27+
hmac = assert openssl_hmac.new(key, "sha256")
28+
29+
hmac\update str
30+
assert hmac\final!
31+
else
32+
-> error "Either luaossl or resty.openssl is required to calculate hmac sha256 digest"
33+
2234

2335
digest_sha256 = (str) ->
2436
digest = assert require("openssl.digest").new("sha256")

0 commit comments

Comments
 (0)