{"id":13896701,"url":"https://github.com/luvit/lua-openssl","last_synced_at":"2025-07-17T13:30:52.863Z","repository":{"id":23520459,"uuid":"26886697","full_name":"luvit/lua-openssl","owner":"luvit","description":"Openssl binding for Lua, which have LuaCrypto-compat module...","archived":false,"fork":true,"pushed_at":"2015-06-30T16:40:55.000Z","size":1707,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-11-25T02:33:17.095Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"zhaozg/lua-openssl","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luvit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-19T23:30:55.000Z","updated_at":"2020-10-29T05:27:06.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/luvit/lua-openssl","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/luvit/lua-openssl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luvit%2Flua-openssl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luvit%2Flua-openssl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luvit%2Flua-openssl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luvit%2Flua-openssl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luvit","download_url":"https://codeload.github.com/luvit/lua-openssl/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luvit%2Flua-openssl/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265611085,"owners_count":23797809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-06T18:03:05.905Z","updated_at":"2025-07-17T13:30:52.465Z","avatar_url":"https://github.com/luvit.png","language":"Lua","funding_links":[],"categories":["Lua"],"sub_categories":[],"readme":"lua-openssl toolkit - A free, MIT-licensed OpenSSL binding for Lua.\n\n[![Build Status](https://travis-ci.org/zhaozg/lua-openssl.svg)](https://travis-ci.org/zhaozg/lua-openssl)\n[![Build status](https://ci.appveyor.com/api/projects/status/f8xchhlj035yqq88/branch/master?svg=true)](https://ci.appveyor.com/project/zhaozg/lua-openssl/branch/master)\n\n#Index\n\n1. [Introduction](#introduction)\n2. [Documentation](#documentation)\n2. [Howto](#a---howto)\n3. [Examples](#b--example-usage) \n\n# Introduction\n\nI needed a full OpenSSL binding for Lua, after googled, I couldn't find a version to fit my needs.\nI found the PHP openssl binding is a good implementation, and it inspired me.\nSo I decided to write this OpenSSL toolkit for Lua.\n\nThe goal is to fully support the listed items.Below you can find the development progress of lua-openssl. \n\n* Symmetrical encrypt/decrypt. (Finished)\n* Message digest. (Finished)\n* Asymmetrical encrypt/decrypt/sign/verify/seal/open. (Finished)\n* X509 certificate. (Finished)\n* PKCS7/CMS. (Developing)\n* SSL/TLS. (Finished)\n\nMost of the lua-openssl functions require a key or certificate as argument; to make things easy to use OpenSSL,\nThis rule allow you to specify certificates or keys in the following ways:\n\n1. As an openssl.x509 object returned from openssl.x509.read\n2. As an openssl.evp_pkey object return from openssl.pkey.read or openssl.pkey.new\n\nSimilarly, you can also specify a public key as a key object returned from x509:get_public().\n\n## lua-openssl modules\ndigest,cipher, x509, cms and so on, be write as modules.\n\n```lua\n   local digest = require'openssl'.digest\n   local cipher = require'openssl'.cipher\n   local crypto = require'crypto'\n   local ssl    = require'ssl'\n```\n\ndigest() equals with digest.digest(), same cipher() equals with cipher.cipher().\n\n## documentation\n\nDocument please see [here](http://zhaozg.github.io/lua-openssl/)\n\n## compat with others\n\n**crypto** is a compat module with [LuaCrypto](https://github.com/mkottman/luacrypto),\ndocument should to [reference](http://mkottman.github.io/luacrypto/manual.html#reference)\n\n**ssl** is a compat module with [luasec](https://github.com/brunoos/luasec),\ndocument should to [refrence](https://github.com/brunoos/luasec/wiki/LuaSec-0.5).\nNYI list: conn:settimeout,...\n\n## lua-openssl Objects\n\nThe following are some important lua-openssl object types:\n\n```\n\topenssl.bio,\n\topenssl.x509,\n\topenssl.stack_of_x509,\n\topenssl.x509_req,\n\topenssl.evp_pkey,\n\topenssl.evp_digest,\n\topenssl.evp_cipher,\n\topenssl.engine,\n\topenssl.pkcs7,\n\topenssl.cms,\n\topenssl.evp_cipher_ctx,\n\topenssl.evp_digest_ctx\n\t...\n```\n\nThey are shortened as bio, x509, sk_x509, csr, pkey, digest, cipher,\n\tengine, cipher_ctx, and digest_ctx.\n\n\n## openssl.bn\n* ***openssl.bn*** come from [http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/](http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/),thanks.\n\nopenssl.bn is a big-number library for Lua 5.1. It handles only integers and is\nsuitable for number-theoretical and cryptographic applications. It is based\non the bn subsystem of OpenSSL cryptographic library:\n\thttp://www.openssl.org/docs/crypto/bn.html\nIf you're running Unix, you probably already have OpenSSL installed.\n\nTo try the library, just edit Makefile to reflect your installation of Lua and\nthen run make. This will build the library and run a simple test. For detailed\ninstallation instructions, see\n\thttp://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html\n\nThere is no manual but the library is simple and intuitive; see the summary\nbelow.\n\nbn library:\n```\n __add(x,y)\t\tcompare(x,y)\t\tpow(x,y) \n __div(x,y)\t\tdiv(x,y)\t\t\tpowmod(x,y,m) \n __eq(x,y)\t\tdivmod(x,y)\t\t\trandom(bits) \n __lt(x,y)\t\tgcd(x,y)\t\t\trmod(x,y) \n __mod(x,y)\t\tinvmod(x)\t\t\tsqr(x) \n __mul(x,y)\t\tisneg(x)\t\t\tsqrmod(x) \n __pow(x,y)\t\tisodd(x)\t\t\tsqrtmod(x) \n __sub(x,y)\t\tisone(x)\t\t\tsub(x,y) \n __tostring(x)\tisprime(x,[checks])\tsubmod(x,y,m) \n __unm(x)\t\tiszero(x)\t\t\ttext(t) \n abs(x)\t\t\tmod(x,y)\t\t\ttohex(x) \n add(x,y)\t\tmul(x,y)\t\t\ttonumber(x) \n addmod(x,y,m)\tmulmod(x,y,m)\t\ttostring(x) \n aprime(bits)\tneg(x)\t\t\t\ttotext(x) \n bits(x)\t\tnumber(x)\t\t\tversion \n```\n\n## Version\n\nThis lua-openssl toolkit works with Lua 5.1 or 5.2, and OpenSSL (0.9.8 or above 1.0.0). \nIt is recommended to use the most up-to-date OpenSSL version because of the recent security fixes.\n\nIf you want to get the lua-openssl and OpenSSL versions from a Lua script, here is how:\n\n```lua\nopenssl = require \"openssl\"\nlua_openssl_version, lua_version, openssl_version = openssl.version()\n```\n\n## Bugs\n\nLua-Openssl is heavily updated, if you find bug, please report to [here](https://github.com/zhaozg/lua-openssl/issues/)\n\n#A.   Howto\n\n### Howto 1: Build on Linux/Unix System.\n\n\tmake\n\tmake install\n\tmake clean\n\n### Howto 2: Build on Windows with MSVC.\n\nBefore building, please change the setting in the config.win file.\nWorks with Lua5.1 (should support Lua5.2 by updating the config.win file).\n\n\tnmake -f makefile.win\n\tnmake -f makefile.win install\n\tnmake -f makefile.win clean\n\n\n### Howto 3: Build on Windows with mingw.\n\n\tmake\n\tmake install\n\tmake clean\n\n### Howto 4: Install using luarocks.\n\t\n\tluarocks install openssl --server=https://rocks.moonscript.org/dev\n\n### Howto 5: Handle fail or error\n\n  Most lua-openssl function or methods return nil or false when error or \nfailed, followed by string type error _reason_ and number type error _code_, \n_code_ can pass to openssl.error() to get more error information.\n\n  All SSL object IO operation methods return nil or false when fail or error.\nWhen nil returned, it followed by 'ssl' or 'syscall', means SSL layer or \nsystem layer error. When false returned, it followed by number 0, 'want_read',\n'want_write','want_x509_lookup','want_connect','want_accept'. Numnber 0 means\nSSL connection closed, others means you should do some SSL operation.\n\n  Please remeber that when lua-openssl function or methods failed without \nerror code, you can get last error by openssl.error(), and repeat call \nopenssl.error() will walk through error stacks of current threads. \nopenssl.error(true) will also clear error stacks after get last error code,\nthis is very useful to free memory when lua-openssl repeat calls or run long times.\n\n#B.  Example usage\n\n### Example 1: short encrypt/decrypt\n\n```lua\nlocal evp_cipher = openssl.cipher.get('des')\nm = 'abcdefghick'\nkey = m\ncdata = evp_cipher:encrypt(m,key)\nm1  = evp_cipher:decrypt(cdata,key)\nassert(m==m1)\n```\n\n### Example 2: quick evp_digest\n\n```lua\nmd = openssl.digest.get('md5')\nm = 'abcd'\naa = md:digest(m)\n\nmdc=md:new()\nmdc:update(m)\nbb = mdc:final()\nassert(openssl.hex(aa,true)==bb)\n```\n\n### Example 3:  Iterate a openssl.stack_of_x509(sk_x509) object\n\n```lua\nn = #sk\nfor i=1, n do\n\tx = sk:get(i)\nend\n```\n\n### Example 4: read and parse certificate\n\n```lua\nlocal openssl = require('openssl')\n\nfunction dump(t,i)\n\tfor k,v in pairs(t) do\n\t\tif(type(v)=='table') then\n\t\t\tprint( string.rep('\\t',i),k..'={')\n\t\t\tdump(v,i+1)\n\t\t\tprint( string.rep('\\t',i),k..'=}')\n\t\telse\n\t\t\tprint( string.rep('\\t',i),k..'='..tostring(v))\n\t\tend\n\tend\nend\n\nfunction test_x509()\n\tlocal x = openssl.x509.read(certasstring)\n\tprint(x)\n\tt = x:parse()\n\tdump(t,0)\n\tprint(t)\nend\n\ntest_x509()\n```\n\n###Example 5: bio network handle(TCP)\n\n * server\n \n```lua\nlocal openssl = require'openssl'\nlocal bio = openssl.bio\n\nhost = host or \"127.0.0.1\"; --only ip\nport = port or \"8383\";\n\nlocal srv = assert(bio.accept(host..':'..port))\nprint('listen at:'..port)\nlocal cli = assert(srv:accept())\nwhile 1 do\n    cli = assert(srv:accept())\n    print('CLI:',cli)\n    while cli do\n        local s = assert(cli:read())\n        print(s)\n        assert(cli:write(s))\n    end\n    print(openssl.error(true))\nend\n```\n\n * client\n```lua\nlocal openssl = require'openssl'\nlocal bio = openssl.bio\nio.read()\n\nhost = host or \"127.0.0.1\"; --only ip\nport = port or \"8383\";\n\nlocal cli = assert(bio.connect(host..':'..port,true))\n\n    while cli do\n        s = io.read()\n        if(#s\u003e0) then\n            print(cli:write(s))\n            ss = cli:read()\n            assert(#s==#ss)\n        end\n    end\n    print(openssl.error(true))\n```\n\nFor more examples, please see test lua script file.\n\n\n--------------------------------------------------------------------\n***lua-openssl License***\n\nCopyright (c) 2011 - 2014 zhaozg, zhaozg(at)gmail.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to\ndeal in the Software without restriction, including without limitation the\nrights to use, copy, modify, merge, publish, distribute, sublicense, and/or\nsell copies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n--------------------------------------------------------------------\n\nThis product includes PHP software, freely available from \u003chttp://www.php.net/software/\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluvit%2Flua-openssl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluvit%2Flua-openssl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluvit%2Flua-openssl/lists"}