在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):zhaozg/lua-openssl开源软件地址(OpenSource Url):https://github.com/zhaozg/lua-openssl开源编程语言(OpenSource Language):C 66.9%开源软件介绍(OpenSource Introduction):lua-openssl toolkit - A free, MIT-licensed OpenSSL binding for Lua. IndexIntroductionI needed a full OpenSSL binding for Lua, after googled, I couldn't find a version to fit my needs. I found the PHP openssl binding is a good implementation, and it inspired me. So I decided to write this OpenSSL toolkit for Lua. The goal is to fully support openssl, include:
This lua-openssl toolkit works with Lua 5.1/5.2/5.3/5.4 or luajit 2.0/2.1, and OpenSSL 0.9.8 or above 1.0.0 or LibreSSL. It is recommended to use the most up-to-date OpenSSL version because of the recent security fixes. Most of the lua-openssl functions require a key or certificate as argument, to make things easy to use OpenSSL. This rule allows you to specify certificates or keys in the following ways:
Similarly, you can also specify a public key as a key object returned from
lua-openssl modulesdigest, cipher, x509, pkcs7, cms and so on, be write as modules. local digest = require'openssl'.digest
local cipher = require'openssl'.cipher digest() equals with digest.digest(), same cipher() equals with cipher.cipher(). documentationDocument please see here, that are generate by LDoc. Notice: Document quality is low and stale, feel free to make a PR to improve it. lua-openssl ObjectsThe following are some important lua-openssl object types:
They are shortened as bio, x509, sk_x509, csr, pkey, digest, cipher, engine, cipher_ctx, and digest_ctx. openssl.bn
openssl.bn is a big-number library for Lua 5.1. It handles only integers and is suitable for number-theoretical and cryptographic applications. It is based on the bn subsystem of OpenSSL cryptographic library: https://github.com/openssl/openssl/blob/OpenSSL_1_0_2-stable/crypto/bn/bn.h If you're running Unix, you probably already have OpenSSL installed. To try the library, just edit Makefile to reflect your installation of Lua and then run make. This will build the library and run a simple test. For detailed installation instructions, see http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/index.html#lbn There is no manual but the library is simple and intuitive; see the summary below. bn library:
VersionYou can get version of lua-openssl, lua and OpenSSL from a Lua script. openssl = require "openssl"
-- get version string format
lua_openssl_version, lua_version, openssl_version = openssl.version()
-- get version number format
lua_openssl_version, lua_version, openssl_version = openssl.version(true) StyleSource code of lua-openssl tidy with astyle
BugsLua-Openssl is heavily updated, if you find a bug, please report to here I try to use luaunit to write unit test, and welcome PR to improve it. HowtoHowto 1: Build on Linux/Unix System.
Howto 2: Build on Windows with MSVC.Before building, please change the setting in the config.win file. Works with Lua5.1 (should support Lua5.2 by updating the config.win file).
Howto 3: Build on Windows with mingw.
Howto 4: Install using luarocks.
Howto 5: Build with CMakeBuild shared lua-openssl.
Build static lua-openssl
Howto 5: Handle fail or errorMost lua-openssl function or methods return nil or false when error or failed, followed by string type error reason and number type error code, code can pass to openssl.error() to get more error information. All SSL object IO operation methods return nil or false when fail or error. When nil returned, it followed by 'ssl' or 'syscall', means SSL layer or system layer error. When false returned, it is followed by number 0, 'want_read', 'want_write','want_x509_lookup','want_connect','want_accept'. Number 0 means SSL connection closed, other numbers means you should do some SSL operation. Please remember that when lua-openssl function or methods fail without an error code, you can get the last error by openssl.error(), and repeat call openssl.error() will walk through error stacks of current threads. openssl.errors(true) will also clear error stacks after return all errors, this is very useful to free memory when lua-openssl repeat calls or run long times. Example usageExample 1: short encrypt/decryptlocal evp_cipher = openssl.cipher.get('des')
m = 'abcdefghick'
key = m
cdata = evp_cipher:encrypt(m,key)
m1 = evp_cipher:decrypt(cdata,key)
assert(m==m1) Example 2: quick evp_digestmd = openssl.digest.get('md5')
m = 'abcd'
aa = md:digest(m)
mdc=md:new()
mdc:update(m)
bb = mdc:final()
assert(openssl.hex(aa,true)==bb) Example 3: Quick HMAC hashlocal hmac = require "openssl".hmac
alg = 'sha256'
key = '0123456789'
msg = 'example message'
hmac.hmac(alg, msg, key, true) -- binary/"raw" output
hmac.hmac(alg, msg, key, false) -- hex output Example 4: Iterate a openssl.stack_of_x509(sk_x509) objectn = #sk
for i=1, n do
x = sk:get(i)
end Example 5: read and parse certificatelocal openssl = require('openssl')
function dump(t,i)
for k,v in pairs(t) do
if(type(v)=='table') then
print( string.rep('\t',i),k..'={')
dump(v,i+1)
print( string.rep('\t',i),k..'=}')
else
print( string.rep('\t',i),k..'='..tostring(v))
end
end
end
function test_x509()
local x = openssl.x509.read(certasstring)
print(x)
t = x:parse()
dump(t,0)
print(t)
end
test_x509() Example 5: bio network handle(TCP)
local openssl = require'openssl'
local bio = openssl.bio
host = host or "127.0.0.1"; --only ip
port = port or "8383";
local srv = assert(bio.accept(host..':'..port))
print('listen at:'..port)
local cli = assert(srv:accept())
while 1 do
cli = assert(srv:accept())
print('CLI:',cli)
while cli do
local s = assert(cli:read())
print(s)
assert(cli:write(s))
end
print(openssl.error(true))
end
local openssl = require'openssl'
local bio = openssl.bio
io.read()
host = host or "127.0.0.1"; --only ip
port = port or "8383";
local cli = assert(bio.connect(host..':'..port,true))
while cli do
s = io.read()
if(#s>0) then
print(cli:write(s))
ss = cli:read()
assert(#s==#ss)
end
end
print(openssl.error(true)) For more examples, please see test lua script file. lua-openssl License Copyright (c) 2011 - 2022 zhaozg, zhaozg(at)gmail.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论