对例子进行openssl进行签名验证进行问题修改
This commit is contained in:
parent
e422654320
commit
242391f577
|
|
@ -206,9 +206,9 @@ end
|
||||||
-- 执行示例
|
-- 执行示例
|
||||||
example()
|
example()
|
||||||
|
|
||||||
do
|
--do
|
||||||
return
|
-- return
|
||||||
end
|
--end
|
||||||
|
|
||||||
-- 公钥加密(用于生成测试数据)
|
-- 公钥加密(用于生成测试数据)
|
||||||
local function rsa_encrypt(pub_key, plaintext)
|
local function rsa_encrypt(pub_key, plaintext)
|
||||||
|
|
@ -302,45 +302,36 @@ local function test_rsa_crypto()
|
||||||
-------------------------------
|
-------------------------------
|
||||||
-- 要签名的数据
|
-- 要签名的数据
|
||||||
local md5val = original_text --ngx.md5(original_text) -- 使用MD5作为摘要算法,你也可以使用其他如sha256等
|
local md5val = original_text --ngx.md5(original_text) -- 使用MD5作为摘要算法,你也可以使用其他如sha256等
|
||||||
|
|
||||||
--[[
|
|
||||||
-- 创建签名
|
|
||||||
-- 使用SHA256算法
|
|
||||||
local digest1 = digest.new("sha256")
|
|
||||||
-- 更新签名上下文,提供数据以供签名
|
|
||||||
digest1:update(md5val)
|
|
||||||
-- 完成签名
|
|
||||||
local signature, err = digest1:final(priv_key)
|
|
||||||
if not signature then
|
|
||||||
ngx.log(ngx.ERR, "Failed to generate signature: ", err)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
ngx.say("签名文件: ", signature)
|
|
||||||
local decode_date = ngx.encode_base64(signature)
|
|
||||||
ngx.say("签名加密后(Base64):", decode_date)
|
|
||||||
--]]
|
|
||||||
---- 生成签名------------------
|
---- 生成签名------------------
|
||||||
-- 使用SHA256算法
|
-- 使用SHA256算法
|
||||||
local digestG = digest.new("sha256")
|
local md = digest.new("sha256")
|
||||||
-- 更新签名上下文,提供数据以供签名
|
-- 更新签名上下文,提供数据以供签名
|
||||||
digestG:update(md5val)
|
md:update(md5val)
|
||||||
-- 完成签名
|
-- 完成签名
|
||||||
local signature, err = digestG:final(priv_key)
|
local privK, err = pkey.new(priv_key)
|
||||||
|
if not privK then
|
||||||
|
return nil, "参数错误(私钥或密文为空)"
|
||||||
|
end
|
||||||
|
local signature, err = privK:sign(md)
|
||||||
if not signature then
|
if not signature then
|
||||||
ngx.log(ngx.ERR, "Failed to generate signature: ", err)
|
ngx.log(ngx.ERR, "Failed to generate signature: ", err)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
ngx.say("签名数据: ", signature)
|
ngx.say("签名数据1: ", signature)
|
||||||
local encode_date = ngx.encode_base64(signature)
|
local encode_date = ngx.encode_base64(signature)
|
||||||
ngx.say("签名数据加密后(Base64):", encode_date)
|
ngx.say("签名数据加密后(Base64):", encode_date)
|
||||||
|
|
||||||
---- 验证签名------------------
|
---- 验证签名------------------
|
||||||
local pubK, err = pkey.new(pub_key)
|
local pubK, err = pkey.new(pub_key)
|
||||||
if not pubK then
|
if not pubK then
|
||||||
return nil, "参数错误(私钥或密文为空)"
|
return nil, "参数错误(私钥或密文为空)"
|
||||||
end
|
end
|
||||||
local decode_date = ngx.decode_base64(encode_date)
|
local digestP = digest.new("sha256")
|
||||||
ngx.say("签名数据解密后(Base64):", decode_date)
|
-- 更新签名上下文,提供数据以供签名
|
||||||
local is_valid = pubK:verify(encode_date, md5val) -- 使用与签名相同的摘要算法进行验证
|
digestP:update(md5val)
|
||||||
|
local signature1 = ngx.decode_base64(encode_date)
|
||||||
|
ngx.say("签名数据2: ", signature1)
|
||||||
|
local is_valid = pubK:verify(signature1, digestP) -- 使用与签名相同的摘要算法进行验证
|
||||||
if is_valid then
|
if is_valid then
|
||||||
ngx.say("Signature is valid.")
|
ngx.say("Signature is valid.")
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user