GithubHelp home page GithubHelp logo

lua-resty-rsa's Introduction

Name

lua-resty-rsa - RSA functions for LuaJIT

Status

This library is considered production ready.

Build status: pass

Description

This library requires an nginx build with OpenSSL, the ngx_lua module, and LuaJIT.

Synopsis

    # nginx.conf:

    lua_package_path "/path/to/lua-resty-rsa/lib/?.lua;;";

    server {
        location = /test {
            content_by_lua_file conf/test.lua;
        }
    }

    -- conf/test.lua:

    local resty_rsa = require "resty.rsa"
    local rsa_public_key, rsa_priv_key, err = resty_rsa:generate_rsa_keys(2048)
    if not rsa_public_key then
        ngx.say('generate rsa keys err: ', err)
    end

    ngx.say(rsa_public_key)
    --[[
    -----BEGIN RSA PUBLIC KEY-----
    MIIBCgKCAQEAuw4T755fepEyXTM66pzf6nv8NtnukQTMGnhmBFIFHp/P2vEpxjXU
    BBDUpzKkVFR3wuK9O1FNmRDAGNGYC0N/9cZNdhykA1NixJfKQzncN31VJTmNqJNZ
    W0x7H9ZGoh2aE0zCCZpRlC1Rf5rL0SVlBoQkn/n9LnYFwyLLIK5/d/y/NZVL6Z6L
    cyvga0zRajamLIjY0Dy/8YIwVV6kaSsHeRv2cOB03eam6gbhLGIz/l8wuJhIn1rO
    yJLQ36IOJymbbNmcC7+2hEQJP40qLvH7hZ1LaAkgQUHjfi8RvH2T1Jmce7XGPxCo
    Ed0yfeFz+pL1KeSWNey6cL3N5hJZE8EntQIDAQAB
    -----END RSA PUBLIC KEY-----
    ]]--

    ngx.say(rsa_priv_key)
    --[[
    -----BEGIN RSA PRIVATE KEY-----
    MIIEpAIBAAKCAQEAuw4T755fepEyXTM66pzf6nv8NtnukQTMGnhmBFIFHp/P2vEp
    xjXUBBDUpzKkVFR3wuK9O1FNmRDAGNGYC0N/9cZNdhykA1NixJfKQzncN31VJTmN
    qJNZW0x7H9ZGoh2aE0zCCZpRlC1Rf5rL0SVlBoQkn/n9LnYFwyLLIK5/d/y/NZVL
    6Z6Lcyvga0zRajamLIjY0Dy/8YIwVV6kaSsHeRv2cOB03eam6gbhLGIz/l8wuJhI
    n1rOyJLQ36IOJymbbNmcC7+2hEQJP40qLvH7hZ1LaAkgQUHjfi8RvH2T1Jmce7XG
    PxCoEd0yfeFz+pL1KeSWNey6cL3N5hJZE8EntQIDAQABAoIBAGim1ayIFK8EMQNH
    uDyui/Aqcc9WWky0PGTK23irUsXxb1708gQ89WNY70Cj6qBrqZ1VMb3QHPP4FSFN
    kh0rJJoi2g+ssm5R5r5KlhTKeFRrQInVC1Y3KhUUUwZa4aWtnhgSJ7Urq1yVhjU4
    K7PVkhH1OHBwcp/d1Bd6jd65AgPkY63P+WpcARJkClmQ1RhgoRwThyJdpKrV4/gO
    ha0AUGlJNRNvRwiZxP0zaI5C8RdrG96SnVpeYOcD0z/M1HVlkoYMXsXLKttwLfpK
    88Igtm6ZJwRpfuMF5VA+9hHaYGCBdGz0B/rMp2fc+EtrOavYQGrWIWi2RL1Qk6Rt
    BUyeTgECgYEA9anj4n/cak1MT+hbNFsL31mJXryl1eVNjEZj/iPMztpdS15CmFgj
    Kjr9UuintjSiK7Is43nZUWWyP1XQjRhVi2uP7PRIv92QNl/YteWD6tYCInJHKe2J
    QqYyZrElezsdayXb5DK6bi1UIYYji90g79N7x6pOR0UnQNQUXTv+Y8ECgYEAwuzl
    6Ez4BSXIIL9NK41jfNMa73Utfl5oO1f6mHM2KbILqaFE76PSgEeXDbOKdcjCbbqC
    KCGjwyPd+Clehg4vkYXTq1y2SQGHwfz7DilPSOxhPY9ND7lGbeNzDUK4x8xe52hd
    MWKdgqeqCK83e5D0ihzRiMah8dbxmlfLAOZ3sPUCgYEA0dT9Czg/YqUHq7FCReQG
    rg3iYgMsexjTNh/hxO97PqwRyBCJPWr7DlU4j5qdteobIsubv+kSEI6Ww7Ze3kWM
    u/tyAeleQlPTnD4d8rBKD0ogpJ+L3WpBNaaToldpNmr149GAktgpmXYqSEA1GIAW
    ZAL11UPIfOO6dYswobpevYECgYEApSosSODnCx2PbMgL8IpWMU+DNEF6sef2s8oB
    aam9zCi0HyCqE9AhLlb61D48ZT8eF/IAFVcjttauX3dWQ4rDna/iwgHF5yhnyuS8
    KayxJJ4+avYAmwEnfzdJpoPRpGI0TCovRQhFZI8C0Wb+QTJ7Mofmt9lvIUc64sff
    GD0wT/0CgYASMf708dmc5Bpzcis++EgMJVb0q+ORmWzSai1NB4bf3LsNS6suWNNU
    zj/JGtMaGvQo5vzGU4exNkhpQo8yUU5YbHlA8RCj7SYkmP78kCewEqxlx7dbcuj2
    LAPWpiDca8StTfEphoKEVfCPHaUk0MlBHR4lCrnAkEtz23vhZKWhFw==
    -----END RSA PRIVATE KEY-----
    ]]--

    local pub, err = resty_rsa:new({ public_key = rsa_public_key })
    if not pub then
        ngx.say("new rsa err: ", err)
        return
    end
    local encrypted, err = pub:encrypt("hello")
    if not encrypted then
        ngx.say("failed to encrypt: ", err)
        return
    end
    ngx.say("encrypted length: ", #encrypted)

    local priv, err = resty_rsa:new({ private_key = rsa_priv_key })
    if not priv then
        ngx.say("new rsa err: ", err)
        return
    end
    local decrypted = priv:decrypt(encrypted)
    ngx.say(decrypted == "hello")

    local algorithm = "SHA256"
    local priv, err = resty_rsa:new({ private_key = rsa_priv_key, algorithm = algorithm })
    if not priv then
        ngx.say("new rsa err: ", err)
        return
    end

    local str = "hello"
    local sig, err = priv:sign(str)
    if not sig then
        ngx.say("failed to sign:", err)
        return
    end
    ngx.say("sig length: ", #sig)

    local pub, err = resty_rsa:new({ public_key = rsa_public_key, algorithm = algorithm })
    if not pub then
        ngx.say("new rsa err: ", err)
        return
    end
    local verify, err = pub:verify(str, sig)
    if not verify then
        ngx.say("verify err: ", err)
        return
    end
    ngx.say(verify)

Methods

To load this library,

  1. you need to specify this library's path in ngx_lua's lua_package_path directive. For example, lua_package_path "/path/to/lua-resty-rsa/lib/?.lua;;";.
  2. you use require to load the library into a local Lua variable:
    local rsa = require "resty.rsa"

generate_rsa_keys

syntax: public_key, private_key, err = rsa:generate_rsa_keys(bits, in_pkcs8_fmt)

Generate rsa public key and private key by specifying the number of bits. The in_pkcs8_fmt is optional. If in_pkcs8_fmt is true, the generated priviate key is in PKCS#8 format and the public key is in PKIX format, which start with -----BEGIN PUBLIC or -----BEGIN PRIVATE. Otherwise the generated keys are in PKCS#1 format, which start with -----BEGIN RSA.

new

syntax: obj, err = rsa:new(opts)

Creates a new rsa object instance by specifying an options table opts.

The options table accepts the following options:

  • public_key Specifies the public rsa key.
  • private_key Specifies the private rsa key.
  • password Specifies the password to read rsa key.
  • key_type Specifies the type of given key. By default the type will be detected from the value of the key.
key_type value meaning
rsa.KEY_TYPE.PKCS1 The input key is in PKCS#1 format(usually starts with -----BEGIN RSA PUBLIC).
rsa.KEY_TYPE.PKIX The input key is in PKIX format(usually starts with -----BEGIN PUBLIC).
-- creates a rsa object with PKIX format of public key
local resty_rsa = require "resty.rsa"
local pub, err = resty_rsa:new({
    public_key = RSA_PKCS8_PUB_KEY,
    key_type = resty_rsa.KEY_TYPE.PKIX,
})

-- creates a rsa object with pkcs#8 format of private key
local priv, err = resty_rsa:new({
    private_key = RSA_PKCS8_PASS_PRIV_KEY,
    key_type = resty_rsa.KEY_TYPE.PKCS8,
    -- you need to specify the password if the pkey is encrypted
    -- password = "foobar",
})
  • padding Specifies the padding mode when you want to encrypt/decrypt.
  • algorithm Specifies the digest algorithm when you want to sign/verify.
algorithm value meaning
md4/MD4/RSA-MD4/md4WithRSAEncryption digest with md4
md5/MD5/RSA-MD5/md5WithRSAEncryption/ssl3-md5 digest with md5
ripemd160/RIPEMD160/RSA-RIPEM160/ripemd160WithRSA/rmd160 digest with ripemd160
sha1/SHA1/RSA-SHA1/sha1WithRSAEncryption/ssl3-sha1 digest with sha1
sha224/SHA224/RSA-SHA224/sha224WithRSAEncryption digest with sha224
sha256/SHA256/RSA-SHA256/sha256WithRSAEncryption digest with sha256
sha384/SHA384/RSA-SHA384/sha384WithRSAEncryption digest with sha384
sha512/SHA512/RSA-SHA512/sha512WithRSAEncryption digest with sha512

encrypt

syntax: encrypted, err = obj:encrypt(str)

decrypt

syntax: decrypted, err = obj:decrypt(encrypted)

sign

syntax: signature, err = obj:sign(str)

verify

syntax: ok, err = obj:verify(str, signature)

Performance

I got the result:

encrypt for 50000 times cost : 2.4110000133514s
decrypt for 50000 times cost : 57.196000099182s
sign for 50000 times cost : 59.169999837875s
verify for 50000 times cost : 1.8230001926422s

when I run this script.

local resty_rsa = require "resty.rsa"
local algorithm = "SHA256"

local rsa_public_key, rsa_priv_key, err = resty_rsa:generate_rsa_keys(2048)
if not rsa_public_key then
    ngx.say("generate rsa keys err: ", err)
    return
end

local pub, err = resty_rsa:new({
    public_key = rsa_public_key,
    padding = resty_rsa.PADDING.RSA_PKCS1_PADDING,
    algorithm = algorithm,
})
if not pub then
    ngx.say("new rsa err: ", err)
    return
end

local priv, err = resty_rsa:new({
    private_key = rsa_priv_key,
    padding = resty_rsa.PADDING.RSA_PKCS1_PADDING,
    algorithm = algorithm,
})
if not priv then
    ngx.say("new rsa err: ", err)
    return
end


local num = 5 * 10000

local str = "hello test"

local encrypted, decrypted, err, sig, verify

ngx.update_time()
local now = ngx.now()

local function timer(operation)
    ngx.update_time()
    local t = ngx.now()

    ngx.say(operation, " for ", num, " times cost : ", t - now, "s")
    now = t
end

for _ = 1, num do
    encrypted, err = pub:encrypt(str)
    if not encrypted then
        ngx.say("failed to encrypt: ", err)
        return
    end
end

timer("encrypt")

for _ = 1, num do
    decrypted = priv:decrypt(encrypted)
    if decrypted ~= str then
        ngx.say("decrypted not match")
        return
    end
end

timer("decrypt")

for _ = 1, num do
    sig, err = priv:sign(str)
    if not sig then
        ngx.say("failed to sign:", err)
        return
    end
end

timer("sign")

for _ = 1, num do
    verify, err = pub:verify(str, sig)
    if not verify then
        ngx.say("verify err: ", err)
        return
    end
end

timer("verify")

Author

Dejiang Zhu (doujiang24) [email protected]

Zexuan Luo (spacewander)

Release Steps

  1. update the _VERSION in lib/resty/rsa.lua
  2. update the version in dist.ini
  3. rename current rockspec to the new version and update the reference in it.
  4. tag the new version
  5. opm upload

Copyright and License

This module is licensed under the MIT license.

Copyright (C) 2014-2018, by Dejiang Zhu (doujiang24) [email protected] Copyright (C) 2018-, by Zexuan Luo (spacewander)

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

See Also

lua-resty-rsa's People

Contributors

doujiang24 avatar fffonion avatar henkvanmaanen avatar spacewander avatar vinayakhulawale avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lua-resty-rsa's Issues

加密后都是乱码

�ԄN%��I��뫔\ �ҢR���"���%E�dtO
��r�N�m��a�I��Ų�]�A_O<���H���A���#_����\��cG�_�xn�{E������+�NxD��?Ą2&0���f�*
��Ŭ�k�9���ƒ5ל�:��XzP�
�!�{y����"�L|}�d��Mԁ�tS�U��;�v�P_�s�kp��+�m��_�(�����58�w�HW� �]�^z̜�_�EP�R՛2�w�,����qӱ�>�>C�T�{�=`�9�<��!���

支付宝开放平台开发助手 生成的公私钥对(pkcs#1)格式问题

lua-resty-rsa生成的 公私钥样例(pkcs#1)如下
image

用支付宝工具生成的公私钥样例(pkcs#1)如下
image

将支付宝私钥(pkcs#1)转成PEM格式后, 做签名一切正常。 但是将支付宝公钥转成PEM个时候,创建rsa公钥对象都失败。

    --  pubkey是用支付宝工具生成的pkcs#1公钥转成PEM格式的公钥
    local pubkey = [[-----BEGIN RSA PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm9HI/Xedl1uKkzaZOTzW
CyAD3zojrXO6XLgmD+CGnwj/gYm2MaeEp1xw+5FKVfUoGF/J8rRzBOY4nslg7JsL
hO3Q8pqa94T942i4DauVgwY+ieDrGuaJ895QaCHHltu62XCVVyXRKmisbZteXJYr
NYyPetdVoaqjoXi+0jQPIvPzOz0y1JFGwvw+kTOvDrN8IlMCrJEpDI7QRlhLTRKV
tv1dRYC8hSjj2VkOVq4uEH+SUc9rvIRt44G6eHWIR1C1HiosjOiY/wT+kfVfkm+Y
qntaOmT9jy20y1A97ZCj9SuTEWGbx9pZvMcVwvh+z63Ds27hvnPssNHyDqjejsCx
hQIDAQAB
-----END RSA PUBLIC KEY-----]];

 local pub, err  = resty_rsa:new({ public_key = pubkey, algorithm = 'SHA256'});
    if not pub then
        return res:json {  msg = err };
    end

错误信息为: "wrong tag: nested asn1 error: nested asn1 error: Field=n, Type=RSA: ASN1 lib"

你好,请问这个问题如何处理?

90 if ffi_cast("void *", rsa) == nil then
91 return err()
92 end
进入return err(),nginx进程就退出了。无法加密解密

我的安装环境(安装了nginx-lua和 luajit的)
[root@Ciaos_Aliyun_01 nginx]# sbin/nginx -V
nginx version: nginx/1.6.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-pcre=../pcre-8.34 --with-zlib=../zlib-1.2.8 --with-http_realip_module --with-http_stub_status_module --with-http_addition_module --with-google_perftools_module --with-openssl=../openssl-1.0.1g --add-module=../ngx_cache_purge-2.1 --add-module=../lua-nginx-module-0.9.8 --with-http_gzip_static_module

[root@Ciaos_Aliyun_01 nginx]# ldd sbin/nginx
linux-vdso.so.1 => (0x00007fffbf3ff000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003496800000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x0000003498c00000)
libluajit-5.1.so.2 => /usr/local/luajit/lib/libluajit-5.1.so.2 (0x00007f8383a1c000)
libm.so.6 => /lib64/libm.so.6 (0x0000003497400000)
libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x000000349cc00000)
libprofiler.so.0 => /usr/lib/libprofiler.so.0 (0x00007f8383809000)
libc.so.6 => /lib64/libc.so.6 (0x0000003496400000)
/lib64/ld-linux-x86-64.so.2 (0x0000003496000000)
libfreebl3.so => /lib64/libfreebl3.so (0x0000003498800000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003496c00000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00000036fea00000)
libz.so.1 => /lib64/libz.so.1 (0x0000003497800000)
libunwind.so.8 => /usr/lib/libunwind.so.8 (0x00007f83835e8000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00000036fe600000)

请问,怎么实现分段加密呢?

要加密的数据比较大(超过256字节),我用java分段加密,lua怎么实现分段解密呢?
麻烦有解决的兄弟,提供一下方案

如何让这个库接受二进制加密内容

我看示例显示解密时候接受的是 字符串,但是现在我的需求是,也就是让下面的参数encrypted 直接使用加密后的二进制内容,或者能接受base64的内容参数也可以啊,因为原始被加密的数据是二进制的,不是明文可读的:
local priv, err = resty_rsa:new({ private_key = rsa_priv_key , key_type = resty_rsa.KEY_TYPE.PKCS8 })
if not priv then
ngx.say("new rsa err: ", err)
return
end
local my_bytes = priv:decrypt(encrypted)

ignoring stale global SSL error

您好,我遇到一个问题,openresty发出了alert信息。信息如下:

23988#0: *21 ignoring stale global SSL error (SSL: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag error:0D06C03A:asn1 encoding routines:ASN1_D2I_EX_PRIMITIVE:nested asn1 error error:0D08303A:asn1 encoding routines:ASN1_TEMPLATE_NOEXP_D2I:nested asn1 error:Field=n, Type=RSA error:04093004:rsa routines:OLD_RSA_PRIV_DECODE:RSA lib), client: 45.251.104.133, server:

我的使用方式:

local resty_rsa = require("resty.rsa")
local private_key = [[
-----BEGIN RSA PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMGd9EohSbkULL68zFoY/AikrTLnvdsUkjN5ulyDWZcDlA4SD/GDTC2Nn5Jz5YjYt+USbsfex/ob2+HfSPtRr3gtCv97xOHjijnt2arfwljxlhTpPLF3bNQJLeeoHpdAX3B+oV1JPcEra4xcTtJRzFIkdduLO3Vye3rFe0UaBonTAgMBAAECgYAkc0o0UoQAle2+kuid5ao9LlN1C+/QONU1X6tYE1pgdcn/xCLAQwmqYcky1yLVWod0Vr4IXsajLW993H1u4vf3gh9TEodNa5L4Ps8mG5MDvP14ZePhKKkrGGpEXQWnJn+mnaNhgumOcnJDngocxvW/VBGjN/XwsdXkUT+p76XwwQJBAN+O9rlSiRSlH/Nizv3PtnGofKd3lxn7QwolPhAOhXYbTEuw0BJ3ubAikLnf2ngJbUrIi2xFyCtaDrhOcHnngMMCQQDdtq/1EPPeCJX6ehOkAoBOMEtf7t6Y0fiKuowlMk02OhNoORDhY/ESnOPkNo5CCM/QkaYSPL+/wKVgteotV0GxAkAlYTQjvIcrKojzGxxdF/P7gR6Gqdpb/8Uv1xZPfP4Xx2y0YzYHQHmN19m3GpTe6USa8nlNe0WC6iX37zWjTHAdAkAW+x2n8Dbm18r76HVqtZuo/xaA/hhyjKkPcCCgpmMccM6cKfYW9q4BOOPiPslmDNXFj6NAFq3laUl7CcAZLQcxAkB30sZP/PSjeXtCVxT+3MeDSBNL67HTqFttWespYNRdvgVY/YYdXJh3d+t3ks7znCP0EYzA1f7k1kAot5fW11s
-----END RSA PRIVATE KEY-----
]]
local priv, err = resty_rsa:new({ private_key = private_key })
if not priv then
ngx_log(ERR,"new rsa err:" , err)
end
我可以正常使用 priv:decrypt()方法,而且也可以解密出相应的结果。

请问如何解决alert的消息问题?
不胜感激

Couldn't extract archive, unrecognized filename extension

Is fixed by: #48

See error below:

RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-rsa

#7 sha256:8e140f869c9afba48a99b5bdbfdcadd85370bfdf13d1a224e06edc0c1b694738

#7 1.632 Installing https://luarocks.org/lua-resty-rsa-1.1.0-2.src.rock

#7 2.437 

#7 2.437 Error: Couldn't extract archive /tmp/luarocks_lua-resty-rsa-1.1.0-2-PKcgv1/lua-resty-rsa.git: unrecognized filename extension

#7 ERROR: executor failed running [/bin/sh -c /usr/local/openresty/luajit/bin/luarocks install lua-resty-rsa]: exit code: 1

哥们你的demo有个小bug

-- conf/test.lua:

local resty_rsa = require "resty.rsa"
local rsa_public_key, rsa_priv_key, err = resty_rsa:generate_rsa_keys(2048)  -----这里是rsa_priv_key
if not rsa_public_key then
    ngx.say('generate rsa keys err: ', err)
end

ngx.say(rsa_public_key)
--[[
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAuw4T755fepEyXTM66pzf6nv8NtnukQTMGnhmBFIFHp/P2vEpxjXU
BBDUpzKkVFR3wuK9O1FNmRDAGNGYC0N/9cZNdhykA1NixJfKQzncN31VJTmNqJNZ
W0x7H9ZGoh2aE0zCCZpRlC1Rf5rL0SVlBoQkn/n9LnYFwyLLIK5/d/y/NZVL6Z6L
cyvga0zRajamLIjY0Dy/8YIwVV6kaSsHeRv2cOB03eam6gbhLGIz/l8wuJhIn1rO
yJLQ36IOJymbbNmcC7+2hEQJP40qLvH7hZ1LaAkgQUHjfi8RvH2T1Jmce7XGPxCo
Ed0yfeFz+pL1KeSWNey6cL3N5hJZE8EntQIDAQAB
-----END RSA PUBLIC KEY-----
]]--

ngx.say( rsa_private_key)    ------------------这里为rsa_private_key

rsa_priv_key
rsa_private_key
两个地方变量名称不一样,我说特么怎么输出为空。。。

PATCH: add PKCS#8 key generation

patch:
rsa.lua.patch

API update:

  rsa:generate_rsa_keys(bits,pkcs8)

test:

local rsa = require("resty.rsa")
local pub, prv, err = rsa:generate_rsa_keys(512, true)
print(pub)
print(prv)
print(err)

verify(): verify err: wrong signature length

复现数据如下
公钥:
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwkGMUV0URISM02kn7XAg6Q2RnN2nK+bVaxFPTJ4kApWRNoKy2r2pLyoW4SN3GBNBVroJI6MAOCcv8hcbwUCuMCS07d/tr7n/VxrnWxg7YQ55l+SqDCwPIVapcadNhQK89uG+0d4WNrPfFMln515gGjYHpHlrBwjXRa1gu+mLiN//tzn0URCOpz3bbXIvyR1TqT0FmqllOkv6tOHBZ29grdqBc4QarmwBrlQbxiCAWJ+IJMTXhjROp+aNPZHV5q6stB9phkMnde4Rb2tM4cDizRyYvq8EP9Ev8ahy7qFnoZLglZdo2zQkUKIVDqlojvqjJvRe/NuPk0IC50kjlcQQ0QIDAQAB
-----END PUBLIC KEY-----
算法:MD5
原始数据:9r7e36RUCB96+EyTraTKbNlfd0IkFmxBNvYYyoHyDsDXQfiIas4qISPE36pY7ysDfSVaB3i8NFMXuHyefHXcO0ChLpnNHtWusQrvjkyvWIGb1ioBDGkQbp0BvYzkVV19deqESwdV7+yNFXoGKmXgOxkNkUbxVwU34G6c6r+tEycYZi1WFpD1IxwEK+QjfKs1A/5AdGgZGJdM8odtfm9rC+THq66bzxqG5D6nnL4YSzI+cLIoo8dPvoMqXSopBvfPNeaTTbaKfk+WPaBfP3TJC0mDj+JQGHC4SvIt6wlEaBf3HuNoL9IZsVcpDBQ9/ZB2xwCGEcEPLx4mMVHcXcvt1jMbv5kNHr/iq5ImwwZ2MfU1TCrPOM66AKYkIbtIjArg
签名:jEBunT5tkqESrx+DPXoY+XMnqz9YCNm2+6SxdAKu/LR2q9weIPydYPylwrJRQEUsrh2A6p1soZEt67A38W+COYC3k/8WvHL18Rdm6QJAML4m6ooC3qwRZfuZKGzV3sY1KFpzgch0TTu2OHIXkFOecPMB9SOU9SyY1xYR1raqtCl/c69S+1poq5d220N/r3eRfe6OAUnh/6k2gMyZrvvpJqzYTGvHlXVhNgEElX4qV38nMXi1jCheBJ95bZInvJUoXTjyxDrsFCJuc80+Y1t8gBaSpv6bBLP8daFspsv6onCDTYiiBQKy9i9m6twX/FaBL9XE+KSIoOHU2aWZTyRcNA==
rsa version: 1.1.0
通过其他渠道验证可以通过,但是rsa运行时提示 verify(): verify err: wrong signature length,是我使用不当还是什么情况?请帮忙解决

有时候会一直报 Unknown message digest

环境
openssl version
OpenSSL 1.0.1e-fips 11 Feb 2013

openresty version
nginx version: openresty/1.15.8.1rc1

代码
local algorithm = "SHA256"
local priv, err = resty_rsa:new({ private_key = private_key, algorithm = algorithm })

push current version to opm

Could you please push the current version of lua-resty-rsa to opm? The one I just installed using opm get still has the line C.OpenSSL_add_all_digests() in it, which you have since removed from your git repository.

私钥加密需求(private key encrypt demand)

私钥加密需求(private key encrypt demand)

lua-resty-rsa是一个遵循”公钥加密、私钥解密“的类库,公钥是对外部开放的Key,存在公钥被获后模拟加密请求的风险。为了业务的安全希望能够做到遵循”私钥加密、公钥解密“这种模式,目前lua-resty-rsa并没有支持该场景。希望升级该类库能够支持”私钥加密、公钥解密“这种模式。
(Lua-resty-rsa is a class library that follows the "public key encryption and private key decryption" principle. Public keys are open to the public, and there is a risk of simulating encryption requests after the public key is obtained. For the sake of business security, it is hoped that the "private key encryption and public key decryption" mode can be followed. Currently, lua-resty-rsa does not support this scenario. It is hoped that the class library can be upgraded to support the "private key encryption and public key decryption" mode.)

resty_rsa:new({public_key = rsa_pubilc_key, algorithm = 'SHA1' })中公钥格式问题无报错

resty -e '
local cjson = require("cjson")
local resty_string = require "resty.string"

local find = string.find
local sub = string.sub
local gsub = string.gsub
local resty_rsa = require "resty.rsa"
local RSA_PUBLIC_KEY = [[
-----BEGIN PUBLIC KEY-----
aa
-----END PUBLIC KEY-----
]]

local rsa_public_key,err = string.gsub(RSA_PUBLIC_KEY, "aa", "-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEAAQUAA4GNADCBiQKBgQC3f/TBa4c+jMjYiHJQAuW4Ay6m Y1KnIPAUeKMVdaZgeW+1f0SCpCT9g1AVhgbQ2coeoNmLsL/16xqs9QccFDvJzs7k YKcHI/STLKTvaEXFt3VTbnl+Sxq96L4bX7StBq9qS1Yo+cdzshsFQ/5rCJ2sy8pR zHPoC9+Vb0t+MrN4fQIDAQAB -----END PUBLIC KEY-----")
ngx.say("rsa_public_key: ", rsa_public_key)

local pub, err = resty_rsa:new({ public_key = rsa_public_key, algorithm = 'SHA1' })
ngx.say("new rsa err: ", err)
if not pub then
ngx.say("new rsa err: ", err)
end
local verify, err = pub:verify("ssss", "xxxxxxxxxxx")
if not verify then
ngx.say("verify err: ", err)
return
end
ngx.say(verify)

'
我使用上面的测试代码,测试后,结果只打印了
rsa_public_key: -----BEGIN PUBLIC KEY-----
-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEAAQUAA4GNADCBiQKBgQC3f/TBa4c+jMjYiHJQAuW4Ay6m Y1KnIPAUeKMVdaZgeW+1f0SCpCT9g1AVhgbQ2coeoNmLsL/16xqs9QccFDvJzs7k YKcHI/STLKTvaEXFt3VTbnl+Sxq96L4bX7StBq9qS1Yo+cdzshsFQ/5rCJ2sy8pR zHPoC9+Vb0t+MrN4fQIDAQAB -----END PUBLIC KEY-----
-----END PUBLIC KEY-----

公钥有问题的情况下,似乎直接退出了,而没有报错信息返回。

你好,为何我用generate出来的rsa_key可以new成功,而用自己的就wrong tag呢?

这是generate出来的
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAtv4b3uiZ95VbrmP1aHBa+dnCi1ji8zckQba5815A42DwKGZiDqwm
Q7B9BAXKSWyCOCqRd8f39LYuz4fAPuIXWLwzjXkrdYv2wcvOr+8NpIQ05/Kwy8kt
o3Pa3h3Q1Vw7ItWznnz28t6ejPrc0L1l3N/HkMrZnyH23x8FJ8o6k1FhBH28YpuC
9tg4KSbTiOnuHZ/TPnJ2hammRmFEKRpf1PV2lySjVrHXSAQ9+Sr4TwvQJGo9vWCx
QjIvgoD06l8NvnLPu4lsYI8BAkMIsd5rzIp6XYyEHV88+wx6e/AmYf1P4Tkd9XZ3
d8NdWkMd8wJgzncrCnYhmBA/6Wm7orINhwIDAQAB
-----END RSA PUBLIC KEY-----

这是我的:
-----BEGIN RSA PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlKp7UqlyK65w6k3qH99W
rp7xtiCNy1uHTX67nVadoOoMCDGXYJpdmQ8bZ3WQRPu0Xw9/s58c/F2BKLD+jAy9
mFZOyxfVCwgKww4mcKWwFApRG3tHwJaDdlm/iXQdpBB/X2mFk3CXmDNK086NXmJX
cMqIQKMxmfbEqSSAIdKzLK4gNrakP2i4/tNZt2yDvv458EuY+BfB+qYFl0LjVS9m
JoBbXH2dvRtljIrzw0906cPcd+8g/6va0j7beVKIICwrvqop1ziLF51KI5jF/fRf
QlgUKMeypwxXhkBcEM6oqPkonutm/8XdPDTXki7+qsCWJNFI2puJkOmYWZ4uGbPQ
hQIDAQAB
-----END RSA PUBLIC KEY-----

用上面的new出来没问题,也可以正常加解密,用我的错误 new rsa err: wrong tag

公钥BEGIN LINE关键字错误

接口中输入的公钥抬头为:
-----BEGIN RSA PUBLIC KEY-----
然而openssl生成的公钥抬头为:
-----BEGIN PUBLIC KEY-----
没有RSA关键字,只有私钥里才有,希望更正一下。

兄弟能不能加一下签名和验签方法

这里有一位朋友已经添加上了,不过也没有给用例。签名中他默认使用SHA256,而我还想可选SHA1
还有,用其它语言,同一个私钥,签名结果不一致。
您能不能完善一下您的类,加上签名和验签方法?非常感谢。

需求

可否增加一个函数,判断传入的 证书 / 私钥 是否合法,合法 返回证书的过期时间等信息

rsa.lua:149: cannot resolve symbol 'RSA_new'

When I do as your example in README.md,I got the error as below:

2018/04/24 19:57:26 [error] 125048#124724: *47909566 lua entry thread aborted: runtime error: ...y_For_Windows_1.13.5.1001_64Bit\x64\nginx//resty\rsa.lua:149: cannot resolve symbol 'RSA_new': 找不到指定的程序。

stack traceback:
coroutine 0:
[C]: in function '__index'
...y_For_Windows_1.13.5.1001_64Bit\x64\nginx//resty\rsa.lua:149: in function 'generate_rsa_keys'

get a nil from new()

I use a multiline string to init a new pubkey and got 'bad end line ' ERR .
example:

pubkey = [[-----BEGIN PUBLIC KEY-----
asdfasdfasdfasdfasdfasdfasdfasdfas
gafasdfasdfasdfasdfasdfasdf
dfasdfasdfasdfas
fasdfasdfasdfasdfas
-----END PUBLIC KEY-----]]

pub , err = rsa:new({public_key = pubkey})

then pub is nil , err is 'bad end line'

does the opt value require specific format ?
thanks a lot

no start line

定义pubkey,resty_rsa:new(RSA_PUBLIC_KEY, true);

返回错误 no start line

archlinux EVP_PKEY_size error

2024/01/05 11:02:43 [error] 5776#0: *1 lua entry thread aborted: runtime error: 
/opt/openresty/site/lualib/resty/rsa.lua:388: 
/opt/openresty/luajit/lib/libluajit-5.1.so.2: undefined symbol: EVP_PKEY_size

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.