-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathraw-sign-and-recover.lua
37 lines (24 loc) · 1.01 KB
/
raw-sign-and-recover.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
local pkey = require("resty.openssl.pkey")
-- sign_raw and verify_recover for RSA keys
local priv = assert(pkey.new())
local pub = assert(pkey.new(priv:to_PEM("public")))
local original = "original text"
-- same as nodejs: crypto.privateEncrypt
-- php: openssl_private_encrypt
local signed = assert(priv:sign_raw(original))
print("Signed message: " .. ngx.encode_base64(signed))
-- same as nodejs: crypto.publicDecrypt
-- php: openssl_public_decrypt
local recovered = assert(pub:verify_recover(signed))
print("Recovered message: " .. recovered)
-- sign_raw and verify_raw for non RSA keys
local priv = assert(pkey.new({
type = "EC",
}))
local pub = assert(pkey.new(priv:to_PEM("public")))
local md_alg = "sha512"
local hashed = require "resty.openssl.digest".new(md_alg):final(original)
local signed = assert(priv:sign_raw(hashed))
print("Signed message: " .. ngx.encode_base64(signed))
local verified = assert(pub:verify_raw(signed, hashed, md_alg))
print("Verification result: ", verified)