pax_global_header00006660000000000000000000000064123203215450014507gustar00rootroot0000000000000052 comment=e363ac306db6135bdda9399511742fe36ebd4cbb lua-resty-string-0.09/000077500000000000000000000000001232032154500146705ustar00rootroot00000000000000lua-resty-string-0.09/.gitignore000066400000000000000000000000461232032154500166600ustar00rootroot00000000000000*.swp *.swo *~ go t/servroot/ reindex lua-resty-string-0.09/Makefile000066400000000000000000000006431232032154500163330ustar00rootroot00000000000000OPENRESTY_PREFIX=/usr/local/openresty PREFIX ?= /usr/local LUA_INCLUDE_DIR ?= $(PREFIX)/include LUA_LIB_DIR ?= $(PREFIX)/lib/lua/$(LUA_VERSION) INSTALL ?= install .PHONY: all test install all: ; install: all $(INSTALL) -d $(DESTDIR)/$(LUA_LIB_DIR)/resty $(INSTALL) lib/resty/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/resty test: all PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$$PATH prove -I../test-nginx/lib -r t lua-resty-string-0.09/README.markdown000066400000000000000000000144041232032154500173740ustar00rootroot00000000000000Name ==== lua-resty-string - String utilities and common hash functions for ngx_lua and LuaJIT Table of Contents ================= * [Name](#name) * [Status](#status) * [Description](#description) * [Synopsis](#synopsis) * [Author](#author) * [Copyright and License](#copyright-and-license) * [See Also](#see-also) Status ====== This library is considered experimental and still under active development. The API is still in flux and may change without notice. Description =========== This library requires an nginx build with OpenSSL, the [ngx_lua module](http://wiki.nginx.org/HttpLuaModule), and [LuaJIT 2.0](http://luajit.org/luajit.html). Synopsis ======== ```lua # nginx.conf: lua_package_path "/path/to/lua-resty-string/lib/?.lua;;" server { location = /test { content_by_lua_file conf/test.lua; } } -- conf/test.lua: local resty_sha1 = require "resty.sha1" local sha1 = resty_sha1:new() if not sha1 then ngx.say("failed to create the sha1 object") return end local ok = sha1:update("hello, ") if not ok then ngx.say("failed to add data") return end ok = sha1:update("world") if not ok then ngx.say("failed to add data") return end local digest = sha1:final() -- binary digest local str = require "resty.string" ngx.say("sha1: ", str.to_hex(digest)) -- output: "sha1: b7e23ec29af22b0b4e41da31e868d57226121c84" local resty_md5 = require "resty.md5" local md5 = resty_md5:new() if not md5 then ngx.say("failed to create md5 object") return end local ok = md5:update("hel") if not ok then ngx.say("failed to add data") return end ok = md5:update("lo") if not ok then ngx.say("failed to add data") return end local digest = md5:final() local str = require "resty.string" ngx.say("md5: ", str.to_hex(digest)) -- yield "md5: 5d41402abc4b2a76b9719d911017c592" local resty_sha224 = require "resty.sha224" local str = require "resty.string" local sha224 = resty_sha224:new() ngx.say(sha224:update("hello")) local digest = sha224:final() ngx.say("sha224: ", str.to_hex(digest)) local resty_sha256 = require "resty.sha256" local str = require "resty.string" local sha256 = resty_sha256:new() ngx.say(sha256:update("hello")) local digest = sha256:final() ngx.say("sha256: ", str.to_hex(digest)) local resty_sha512 = require "resty.sha512" local str = require "resty.string" local sha512 = resty_sha512:new() ngx.say(sha512:update("hello")) local digest = sha512:final() ngx.say("sha512: ", str.to_hex(digest)) local resty_sha384 = require "resty.sha384" local str = require "resty.string" local sha384 = resty_sha384:new() ngx.say(sha384:update("hel")) ngx.say(sha384:update("lo")) local digest = sha384:final() ngx.say("sha384: ", str.to_hex(digest)) local resty_random = require "resty.random" local str = require "resty.string" local random = resty_random.bytes(16) -- generate 16 bytes of pseudo-random data ngx.say("pseudo-random: ", str.to_hex(random)) local resty_random = require "resty.random" local str = require "resty.string" local strong_random = resty_random.bytes(16,true) -- attempt to generate 16 bytes of -- cryptographically strong random data while strong_random == nil do strong_random = resty_random.bytes(16,true) end ngx.say("random: ", str.to_hex(strong_random)) local aes = require "resty.aes" local str = require "resty.string" local aes_128_cbc_md5 = aes:new("AKeyForAES") -- the default cipher is AES 128 CBC with 1 round of MD5 -- for the key and a nil salt local encrypted = aes_128_cbc_md5:encrypt("Secret message!") ngx.say("AES 128 CBC (MD5) Encrypted HEX: ", str.to_hex(encrypted)) ngx.say("AES 128 CBC (MD5) Decrypted: ", aes_128_cbc_md5:decrypt(encrypted)) local aes = require "resty.aes" local str = require "resty.string" local aes_256_cbc_sha512x5 = aes:new("AKeyForAES-256-CBC", "MySalt!", aes.cipher(256,"cbc"), aes.hash.sha512, 5) -- AES 256 CBC with 5 rounds of SHA-512 for the key -- and a salt of "MySalt!" local encrypted = aes_256_cbc_sha512x5:encrypt("Really secret message!") ngx.say("AES 256 CBC (SHA-512, salted) Encrypted HEX: ", str.to_hex(encrypted)) ngx.say("AES 256 CBC (SHA-512, salted) Decrypted: ", aes_256_cbc_sha512x5:decrypt(encrypted)) ``` [Back to TOC](#table-of-contents) Author ====== Yichun "agentzh" Zhang (章亦春) [Back to TOC](#table-of-contents) Copyright and License ===================== This module is licensed under the BSD license. Copyright (C) 2012-2014, by Yichun "agentzh" Zhang (章亦春) , CloudFlare Inc. 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. [Back to TOC](#table-of-contents) See Also ======== * the ngx_lua module: http://wiki.nginx.org/HttpLuaModule [Back to TOC](#table-of-contents) lua-resty-string-0.09/lib/000077500000000000000000000000001232032154500154365ustar00rootroot00000000000000lua-resty-string-0.09/lib/resty/000077500000000000000000000000001232032154500166045ustar00rootroot00000000000000lua-resty-string-0.09/lib/resty/aes.lua000066400000000000000000000142331232032154500200620ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) --local asn1 = require "resty.asn1" local ffi = require "ffi" local ffi_new = ffi.new local ffi_gc = ffi.gc local ffi_str = ffi.string local ffi_copy = ffi.copy local C = ffi.C local setmetatable = setmetatable local error = error local type = type local _M = { _VERSION = '0.09' } local mt = { __index = _M } ffi.cdef[[ typedef struct engine_st ENGINE; typedef struct evp_cipher_st EVP_CIPHER; typedef struct evp_cipher_ctx_st { const EVP_CIPHER *cipher; ENGINE *engine; int encrypt; int buf_len; unsigned char oiv[16]; unsigned char iv[16]; unsigned char buf[32]; int num; void *app_data; int key_len; unsigned long flags; void *cipher_data; int final_used; int block_mask; unsigned char final[32]; } EVP_CIPHER_CTX; typedef struct env_md_ctx_st EVP_MD_CTX; typedef struct env_md_st EVP_MD; const EVP_MD *EVP_md5(void); const EVP_MD *EVP_sha(void); const EVP_MD *EVP_sha1(void); const EVP_MD *EVP_sha224(void); const EVP_MD *EVP_sha256(void); const EVP_MD *EVP_sha384(void); const EVP_MD *EVP_sha512(void); const EVP_CIPHER *EVP_aes_128_ecb(void); const EVP_CIPHER *EVP_aes_128_cbc(void); const EVP_CIPHER *EVP_aes_128_cfb1(void); const EVP_CIPHER *EVP_aes_128_cfb8(void); const EVP_CIPHER *EVP_aes_128_cfb128(void); const EVP_CIPHER *EVP_aes_128_ofb(void); const EVP_CIPHER *EVP_aes_128_ctr(void); const EVP_CIPHER *EVP_aes_192_ecb(void); const EVP_CIPHER *EVP_aes_192_cbc(void); const EVP_CIPHER *EVP_aes_192_cfb1(void); const EVP_CIPHER *EVP_aes_192_cfb8(void); const EVP_CIPHER *EVP_aes_192_cfb128(void); const EVP_CIPHER *EVP_aes_192_ofb(void); const EVP_CIPHER *EVP_aes_192_ctr(void); const EVP_CIPHER *EVP_aes_256_ecb(void); const EVP_CIPHER *EVP_aes_256_cbc(void); const EVP_CIPHER *EVP_aes_256_cfb1(void); const EVP_CIPHER *EVP_aes_256_cfb8(void); const EVP_CIPHER *EVP_aes_256_cfb128(void); const EVP_CIPHER *EVP_aes_256_ofb(void); void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a); int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a); int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl, unsigned char *key, const unsigned char *iv); int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl); int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl); int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl, unsigned char *key, const unsigned char *iv); int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl); int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl); int EVP_BytesToKey(const EVP_CIPHER *type,const EVP_MD *md, const unsigned char *salt, const unsigned char *data, int datal, int count, unsigned char *key,unsigned char *iv); ]] local ctx_ptr_type = ffi.typeof("EVP_CIPHER_CTX[1]") local hash hash = { md5 = C.EVP_md5(), sha1 = C.EVP_sha1(), sha224 = C.EVP_sha224(), sha256 = C.EVP_sha256(), sha384 = C.EVP_sha384(), sha512 = C.EVP_sha512() } _M.hash = hash local cipher cipher = function (size, _cipher) local _size = size or 128 local _cipher = _cipher or "cbc" local func = "EVP_aes_" .. _size .. "_" .. _cipher if C[func] then return { size=_size, cipher=_cipher, method=C[func]()} else return nil end end _M.cipher = cipher function _M.new(self, key, salt, _cipher, _hash, hash_rounds) local encrypt_ctx = ffi_new(ctx_ptr_type) local decrypt_ctx = ffi_new(ctx_ptr_type) local _cipher = _cipher or cipher() local _hash = _hash or hash.md5 local hash_rounds = hash_rounds or 1 local _cipherLength = _cipher.size/8 local gen_key = ffi_new("unsigned char[?]",_cipherLength) local gen_iv = ffi_new("unsigned char[?]",_cipherLength) if type(_hash) == "table" then if not _hash.iv or #_hash.iv ~= 16 then return nil, "bad iv" end if _hash.method then local tmp_key = _hash.method(key) if #tmp_key ~= _cipherLength then return nil, "bad key length" end ffi_copy(gen_key, tmp_key, _cipherLength) elseif #key ~= _cipherLength then return nil, "bad key length" else ffi_copy(gen_key, key, _cipherLength) end ffi_copy(gen_iv, _hash.iv, 16) else if C.EVP_BytesToKey(_cipher.method, _hash, salt, key, #key, hash_rounds, gen_key, gen_iv) ~= _cipherLength then return nil end end C.EVP_CIPHER_CTX_init(encrypt_ctx) C.EVP_CIPHER_CTX_init(decrypt_ctx) if C.EVP_EncryptInit_ex(encrypt_ctx, _cipher.method, nil, gen_key, gen_iv) == 0 or C.EVP_DecryptInit_ex(decrypt_ctx, _cipher.method, nil, gen_key, gen_iv) == 0 then return nil end ffi_gc(encrypt_ctx, C.EVP_CIPHER_CTX_cleanup) ffi_gc(decrypt_ctx, C.EVP_CIPHER_CTX_cleanup) return setmetatable({ _encrypt_ctx = encrypt_ctx, _decrypt_ctx = decrypt_ctx }, mt) end function _M.encrypt(self, s) local s_len = #s local max_len = s_len + 16 local buf = ffi_new("unsigned char[?]", max_len) local out_len = ffi_new("int[1]") local tmp_len = ffi_new("int[1]") local ctx = self._encrypt_ctx if C.EVP_EncryptInit_ex(ctx, nil, nil, nil, nil) == 0 then return nil end if C.EVP_EncryptUpdate(ctx, buf, out_len, s, s_len) == 0 then return nil end if C.EVP_EncryptFinal_ex(ctx, buf + out_len[0], tmp_len) == 0 then return nil end return ffi_str(buf, out_len[0] + tmp_len[0]) end function _M.decrypt(self, s) local s_len = #s local buf = ffi_new("unsigned char[?]", s_len) local out_len = ffi_new("int[1]") local tmp_len = ffi_new("int[1]") local ctx = self._decrypt_ctx if C.EVP_DecryptInit_ex(ctx, nil, nil, nil, nil) == 0 then return nil end if C.EVP_DecryptUpdate(ctx, buf, out_len, s, s_len) == 0 then return nil end if C.EVP_DecryptFinal_ex(ctx, buf + out_len[0], tmp_len) == 0 then return nil end return ffi_str(buf, out_len[0] + tmp_len[0]) end return _M lua-resty-string-0.09/lib/resty/md5.lua000066400000000000000000000022711232032154500177760ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) local ffi = require "ffi" local ffi_new = ffi.new local ffi_str = ffi.string local C = ffi.C local setmetatable = setmetatable local error = error local _M = { _VERSION = '0.09' } local mt = { __index = _M } ffi.cdef[[ typedef unsigned long MD5_LONG ; enum { MD5_CBLOCK = 64, MD5_LBLOCK = MD5_CBLOCK/4 }; typedef struct MD5state_st { MD5_LONG A,B,C,D; MD5_LONG Nl,Nh; MD5_LONG data[MD5_LBLOCK]; unsigned int num; } MD5_CTX; int MD5_Init(MD5_CTX *c); int MD5_Update(MD5_CTX *c, const void *data, size_t len); int MD5_Final(unsigned char *md, MD5_CTX *c); ]] local buf = ffi_new("char[16]") local ctx_ptr_type = ffi.typeof("MD5_CTX[1]") function _M.new(self) local ctx = ffi_new(ctx_ptr_type) if C.MD5_Init(ctx) == 0 then return nil end return setmetatable({ _ctx = ctx }, mt) end function _M.update(self, s) return C.MD5_Update(self._ctx, s, #s) == 1 end function _M.final(self) if C.MD5_Final(buf, self._ctx) == 1 then return ffi_str(buf, 16) end return nil end function _M.reset(self) return C.MD5_Init(self._ctx) == 1 end return _M lua-resty-string-0.09/lib/resty/random.lua000066400000000000000000000011441232032154500205670ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) local ffi = require "ffi" local ffi_new = ffi.new local ffi_str = ffi.string local C = ffi.C local setmetatable = setmetatable local error = error local _M = { _VERSION = '0.09' } ffi.cdef[[ int RAND_bytes(unsigned char *buf, int num); int RAND_pseudo_bytes(unsigned char *buf, int num); ]] function _M.bytes(len, strong) local buf = ffi_new("char[?]", len) if strong then if C.RAND_bytes(buf, len) == 0 then return nil end else C.RAND_pseudo_bytes(buf,len) end return ffi_str(buf, len) end return _M lua-resty-string-0.09/lib/resty/sha.lua000066400000000000000000000003541232032154500200640ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) local ffi = require "ffi" local _M = { _VERSION = '0.09' } ffi.cdef[[ typedef unsigned long SHA_LONG; typedef unsigned long long SHA_LONG64; enum { SHA_LBLOCK = 16 }; ]]; return _M lua-resty-string-0.09/lib/resty/sha1.lua000066400000000000000000000022621232032154500201450ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) local sha = require "resty.sha" local ffi = require "ffi" local ffi_new = ffi.new local ffi_str = ffi.string local C = ffi.C local setmetatable = setmetatable local error = error local _M = { _VERSION = '0.09' } local mt = { __index = _M } ffi.cdef[[ typedef struct SHAstate_st { SHA_LONG h0,h1,h2,h3,h4; SHA_LONG Nl,Nh; SHA_LONG data[SHA_LBLOCK]; unsigned int num; } SHA_CTX; int SHA1_Init(SHA_CTX *c); int SHA1_Update(SHA_CTX *c, const void *data, size_t len); int SHA1_Final(unsigned char *md, SHA_CTX *c); ]] local digest_len = 20 local buf = ffi_new("char[?]", digest_len) local ctx_ptr_type = ffi.typeof("SHA_CTX[1]") function _M.new(self) local ctx = ffi_new(ctx_ptr_type) if C.SHA1_Init(ctx) == 0 then return nil end return setmetatable({ _ctx = ctx }, mt) end function _M.update(self, s) return C.SHA1_Update(self._ctx, s, #s) == 1 end function _M.final(self) if C.SHA1_Final(buf, self._ctx) == 1 then return ffi_str(buf, digest_len) end return nil end function _M.reset(self) return C.SHA1_Init(self._ctx) == 1 end return _M lua-resty-string-0.09/lib/resty/sha224.lua000066400000000000000000000020421232032154500203100ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) local sha256 = require "resty.sha256" local ffi = require "ffi" local ffi_new = ffi.new local ffi_str = ffi.string local C = ffi.C local setmetatable = setmetatable local error = error local _M = { _VERSION = '0.09' } local mt = { __index = _M } ffi.cdef[[ int SHA224_Init(SHA256_CTX *c); int SHA224_Update(SHA256_CTX *c, const void *data, size_t len); int SHA224_Final(unsigned char *md, SHA256_CTX *c); ]] local digest_len = 28 local buf = ffi_new("char[?]", digest_len) local ctx_ptr_type = ffi.typeof("SHA256_CTX[1]") function _M.new(self) local ctx = ffi_new(ctx_ptr_type) if C.SHA224_Init(ctx) == 0 then return nil end return setmetatable({ _ctx = ctx }, mt) end function _M.update(self, s) return C.SHA224_Update(self._ctx, s, #s) == 1 end function _M.final(self) if C.SHA224_Final(buf, self._ctx) == 1 then return ffi_str(buf, digest_len) end return nil end function _M.reset(self) return C.SHA224_Init(self._ctx) == 1 end return _M lua-resty-string-0.09/lib/resty/sha256.lua000066400000000000000000000023171232032154500203220ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) local sha = require "resty.sha" local ffi = require "ffi" local ffi_new = ffi.new local ffi_str = ffi.string local C = ffi.C local setmetatable = setmetatable local error = error local _M = { _VERSION = '0.09' } local mt = { __index = _M } ffi.cdef[[ typedef struct SHA256state_st { SHA_LONG h[8]; SHA_LONG Nl,Nh; SHA_LONG data[SHA_LBLOCK]; unsigned int num,md_len; } SHA256_CTX; int SHA256_Init(SHA256_CTX *c); int SHA256_Update(SHA256_CTX *c, const void *data, size_t len); int SHA256_Final(unsigned char *md, SHA256_CTX *c); ]] local digest_len = 32 local buf = ffi_new("char[?]", digest_len) local ctx_ptr_type = ffi.typeof("SHA256_CTX[1]") function _M.new(self) local ctx = ffi_new(ctx_ptr_type) if C.SHA256_Init(ctx) == 0 then return nil end return setmetatable({ _ctx = ctx }, mt) end function _M.update(self, s) return C.SHA256_Update(self._ctx, s, #s) == 1 end function _M.final(self) if C.SHA256_Final(buf, self._ctx) == 1 then return ffi_str(buf, digest_len) end return nil end function _M.reset(self) return C.SHA256_Init(self._ctx) == 1 end return _M lua-resty-string-0.09/lib/resty/sha384.lua000066400000000000000000000020421232032154500203170ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) local sha512 = require "resty.sha512" local ffi = require "ffi" local ffi_new = ffi.new local ffi_str = ffi.string local C = ffi.C local setmetatable = setmetatable local error = error local _M = { _VERSION = '0.09' } local mt = { __index = _M } ffi.cdef[[ int SHA384_Init(SHA512_CTX *c); int SHA384_Update(SHA512_CTX *c, const void *data, size_t len); int SHA384_Final(unsigned char *md, SHA512_CTX *c); ]] local digest_len = 48 local buf = ffi_new("char[?]", digest_len) local ctx_ptr_type = ffi.typeof("SHA512_CTX[1]") function _M.new(self) local ctx = ffi_new(ctx_ptr_type) if C.SHA384_Init(ctx) == 0 then return nil end return setmetatable({ _ctx = ctx }, mt) end function _M.update(self, s) return C.SHA384_Update(self._ctx, s, #s) == 1 end function _M.final(self) if C.SHA384_Final(buf, self._ctx) == 1 then return ffi_str(buf, digest_len) end return nil end function _M.reset(self) return C.SHA384_Init(self._ctx) == 1 end return _M lua-resty-string-0.09/lib/resty/sha512.lua000066400000000000000000000025311232032154500203130ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) local sha = require "resty.sha" local ffi = require "ffi" local ffi_new = ffi.new local ffi_str = ffi.string local C = ffi.C local setmetatable = setmetatable local error = error local _M = { _VERSION = '0.09' } local mt = { __index = _M } ffi.cdef[[ enum { SHA512_CBLOCK = SHA_LBLOCK*8 }; typedef struct SHA512state_st { SHA_LONG64 h[8]; SHA_LONG64 Nl,Nh; union { SHA_LONG64 d[SHA_LBLOCK]; unsigned char p[SHA512_CBLOCK]; } u; unsigned int num,md_len; } SHA512_CTX; int SHA512_Init(SHA512_CTX *c); int SHA512_Update(SHA512_CTX *c, const void *data, size_t len); int SHA512_Final(unsigned char *md, SHA512_CTX *c); ]] local digest_len = 64 local buf = ffi_new("char[?]", digest_len) local ctx_ptr_type = ffi.typeof("SHA512_CTX[1]") function _M.new(self) local ctx = ffi_new(ctx_ptr_type) if C.SHA512_Init(ctx) == 0 then return nil end return setmetatable({ _ctx = ctx }, mt) end function _M.update(self, s) return C.SHA512_Update(self._ctx, s, #s) == 1 end function _M.final(self) if C.SHA512_Final(buf, self._ctx) == 1 then return ffi_str(buf, digest_len) end return nil end function _M.reset(self) return C.SHA512_Init(self._ctx) == 1 end return _M lua-resty-string-0.09/lib/resty/string.lua000066400000000000000000000012661232032154500206220ustar00rootroot00000000000000-- Copyright (C) by Yichun Zhang (agentzh) local ffi = require "ffi" local ffi_new = ffi.new local ffi_str = ffi.string local C = ffi.C local setmetatable = setmetatable local error = error local tonumber = tonumber local _M = { _VERSION = '0.09' } ffi.cdef[[ typedef unsigned char u_char; u_char * ngx_hex_dump(u_char *dst, const u_char *src, size_t len); intptr_t ngx_atoi(const unsigned char *line, size_t n); ]] local str_type = ffi.typeof("uint8_t[?]") function _M.to_hex(s) local len = #s * 2 local buf = ffi_new(str_type, len) C.ngx_hex_dump(buf, s, #s) return ffi_str(buf, len) end function _M.atoi(s) return tonumber(C.ngx_atoi(s, #s)) end return _M lua-resty-string-0.09/t/000077500000000000000000000000001232032154500151335ustar00rootroot00000000000000lua-resty-string-0.09/t/aes.t000066400000000000000000000205261232032154500160750ustar00rootroot00000000000000# vi:ft= use Test::Nginx::Socket::Lua; repeat_each(2); plan tests => repeat_each() * (3 * blocks()); our $HttpConfig = <<'_EOC_'; lua_package_path 'lib/?.lua;;'; lua_package_cpath 'lib/?.so;;'; _EOC_ #log_level 'warn'; run_tests(); __DATA__ === TEST 1: AES default hello --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default = aes:new("secret") local encrypted = aes_default:encrypt("hello") ngx.say("AES-128 CBC MD5: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") '; } --- request GET /t --- response_body AES-128 CBC MD5: 7b47a4dbb11e2cddb2f3740c9e3a552b true --- no_error_log [error] === TEST 2: AES empty key hello --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default = aes:new("") local encrypted = aes_default:encrypt("hello") ngx.say("AES-128 (empty key) CBC MD5: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") '; } --- request GET /t --- response_body AES-128 (empty key) CBC MD5: 6cb1a35bf9d66e92c9dec684fc329746 true --- no_error_log [error] === TEST 3: AES 8-byte salt --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default = aes:new("secret","WhatSalt") local encrypted = aes_default:encrypt("hello") ngx.say("AES-128 (salted) CBC MD5: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") '; } --- request GET /t --- response_body AES-128 (salted) CBC MD5: f72db89f8e19326d8da4928be106705c true --- no_error_log [error] === TEST 4: AES oversized 10-byte salt --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default = aes:new("secret","Oversized!") local encrypted = aes_default:encrypt("hello") ngx.say("AES-128 (oversized salt) CBC MD5: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") local aes_check = aes:new("secret","Oversize") local encrypted_check = aes_check:encrypt("hello") ngx.say(encrypted_check == encrypted) '; } --- request GET /t --- response_body AES-128 (oversized salt) CBC MD5: 90a9c9a96f06c597c8da99c37a6c689f true true --- no_error_log [error] === TEST 5: AES-256 ECB SHA1 no salt --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default = aes:new("secret",nil, aes.cipher(256,"ecb"),aes.hash.sha1) local encrypted = aes_default:encrypt("hello") ngx.say("AES-256 ECB SHA1: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") '; } --- request GET /t --- response_body AES-256 ECB SHA1: 927148b31f0e89696a222489403f540d true --- no_error_log [error] === TEST 6: AES-256 ECB SHA1x5 no salt --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default = aes:new("secret",nil, aes.cipher(256,"ecb"),aes.hash.sha1,5) local encrypted = aes_default:encrypt("hello") ngx.say("AES-256 ECB SHA1: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") '; } --- request GET /t --- response_body AES-256 ECB SHA1: d1a9b6e59b8980e783df223889563bee true --- no_error_log [error] === TEST 7: AES-128 CBC custom keygen --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default = aes:new("Xr4ilOzQ4PCOq3aQ0qbuaQ==",nil, aes.cipher(128,"cbc"), {iv = ngx.decode_base64("Jq5cyFTja2vfyjZoSN6muw=="), method = ngx.decode_base64}) local encrypted = aes_default:encrypt("hello") ngx.say("AES-128 CBC (custom keygen) MD5: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") local aes_check = aes:new("secret") local encrypted_check = aes_check:encrypt("hello") ngx.say(encrypted_check == encrypted) '; } --- request GET /t --- response_body AES-128 CBC (custom keygen) MD5: 7b47a4dbb11e2cddb2f3740c9e3a552b true true --- no_error_log [error] === TEST 8: AES-128 CBC custom keygen (without method) --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default = aes:new(ngx.decode_base64("Xr4ilOzQ4PCOq3aQ0qbuaQ=="),nil, aes.cipher(128,"cbc"), {iv = ngx.decode_base64("Jq5cyFTja2vfyjZoSN6muw==")}) local encrypted = aes_default:encrypt("hello") ngx.say("AES-128 CBC (custom keygen) MD5: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") local aes_check = aes:new("secret") local encrypted_check = aes_check:encrypt("hello") ngx.say(encrypted_check == encrypted) '; } --- request GET /t --- response_body AES-128 CBC (custom keygen) MD5: 7b47a4dbb11e2cddb2f3740c9e3a552b true true --- no_error_log [error] === TEST 9: AES-128 CBC custom keygen (without method, bad key len) --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default, err = aes:new("hel", nil, aes.cipher(128,"cbc"), {iv = ngx.decode_base64("Jq5cyFTja2vfyjZoSN6muw==")}) if not aes_default then ngx.say("failed to new: ", err) return end local encrypted = aes_default:encrypt("hello") ngx.say("AES-128 CBC (custom keygen) MD5: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") local aes_check = aes:new("secret") local encrypted_check = aes_check:encrypt("hello") ngx.say(encrypted_check == encrypted) '; } --- request GET /t --- response_body failed to new: bad key length --- no_error_log [error] === TEST 10: AES-128 CBC custom keygen (without method, bad iv) --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" local str = require "resty.string" local aes_default, err = aes:new( ngx.decode_base64("Xr4ilOzQ4PCOq3aQ0qbuaQ=="), nil, aes.cipher(128,"cbc"), {iv = "hello"} ) if not aes_default then ngx.say("failed to new: ", err) return end local encrypted = aes_default:encrypt("hello") ngx.say("AES-128 CBC (custom keygen) MD5: ", str.to_hex(encrypted)) local decrypted = aes_default:decrypt(encrypted) ngx.say(decrypted == "hello") local aes_check = aes:new("secret") local encrypted_check = aes_check:encrypt("hello") ngx.say(encrypted_check == encrypted) '; } --- request GET /t --- response_body failed to new: bad iv --- no_error_log [error] lua-resty-string-0.09/t/atoi.t000066400000000000000000000010251232032154500162520ustar00rootroot00000000000000# vi:ft= use Test::Nginx::Socket::Lua; repeat_each(2); plan tests => repeat_each() * (3 * blocks()); our $HttpConfig = <<'_EOC_'; lua_package_path 'lib/?.lua;;'; lua_package_cpath 'lib/?.so;;'; _EOC_ no_long_string(); run_tests(); __DATA__ === TEST 1: atoi --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local str = require "resty.string" ngx.say(1 + str.atoi("32")) '; } --- request GET /t --- response_body 33 --- no_error_log [error] lua-resty-string-0.09/t/md5.t000066400000000000000000000036011232032154500160050ustar00rootroot00000000000000# vi:ft= use Test::Nginx::Socket::Lua; repeat_each(2); plan tests => repeat_each() * (3 * blocks()); our $HttpConfig = <<'_EOC_'; lua_package_path 'lib/?.lua;;'; lua_package_cpath 'lib/?.so;;'; _EOC_ no_long_string(); run_tests(); __DATA__ === TEST 1: hello MD5 --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_md5 = require "resty.md5" local str = require "resty.string" local md5 = resty_md5:new() ngx.say(md5:update("hello")) local digest = md5:final() ngx.say(digest == ngx.md5_bin("hello")) ngx.say("md5: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true md5: 5d41402abc4b2a76b9719d911017c592 --- no_error_log [error] === TEST 2: MD5 incremental --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_md5 = require "resty.md5" local str = require "resty.string" local md5 = resty_md5:new() ngx.say(md5:update("hel")) ngx.say(md5:update("lo")) local digest = md5:final() ngx.say("md5: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true md5: 5d41402abc4b2a76b9719d911017c592 --- no_error_log [error] === TEST 3: MD5 empty string --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_md5 = require "resty.md5" local str = require "resty.string" local md5 = resty_md5:new() ngx.say(md5:update("")) local digest = md5:final() ngx.say(digest == ngx.md5_bin("")) ngx.say("md5: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true md5: d41d8cd98f00b204e9800998ecf8427e --- no_error_log [error] lua-resty-string-0.09/t/random.t000066400000000000000000000020541232032154500166010ustar00rootroot00000000000000# vi:ft= use Test::Nginx::Socket::Lua; repeat_each(2); plan tests => repeat_each() * (3 * blocks()); our $HttpConfig = <<'_EOC_'; lua_package_path 'lib/?.lua;;'; lua_package_cpath 'lib/?.so;;'; _EOC_ no_long_string(); run_tests(); __DATA__ === TEST 1: pseudo random bytes --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local rand = require "resty.random" local str = require "resty.string" local s = rand.bytes(5) ngx.say("res: ", str.to_hex(s)) '; } --- request GET /t --- response_body_like ^res: [a-f0-9]{10}$ --- no_error_log [error] === TEST 2: strong random bytes --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local rand = require "resty.random" local str = require "resty.string" local s = rand.bytes(5, true) ngx.say("res: ", str.to_hex(s)) '; } --- request GET /t --- response_body_like ^res: [a-f0-9]{10}$ --- no_error_log [error] lua-resty-string-0.09/t/sha1.t000066400000000000000000000037231232032154500161610ustar00rootroot00000000000000# vi:ft= use Test::Nginx::Socket::Lua; repeat_each(2); plan tests => repeat_each() * (3 * blocks()); our $HttpConfig = <<'_EOC_'; #lua_code_cache off; lua_package_path 'lib/?.lua;;'; lua_package_cpath 'lib/?.so;;'; _EOC_ no_long_string(); run_tests(); __DATA__ === TEST 1: hello SHA-1 --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha1 = require "resty.sha1" local str = require "resty.string" local sha1 = resty_sha1:new() ngx.say(sha1:update("hello")) local digest = sha1:final() ngx.say(digest == ngx.sha1_bin("hello")) ngx.say("sha1: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true sha1: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d --- no_error_log [error] === TEST 2: SHA-1 incremental --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha1 = require "resty.sha1" local str = require "resty.string" local sha1 = resty_sha1:new() ngx.say(sha1:update("hel")) ngx.say(sha1:update("lo")) local digest = sha1:final() ngx.say("sha1: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true sha1: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d --- no_error_log [error] === TEST 3: SHA-1 empty string --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha1 = require "resty.sha1" local str = require "resty.string" local sha1 = resty_sha1:new() ngx.say(sha1:update("")) local digest = sha1:final() ngx.say(digest == ngx.sha1_bin("")) ngx.say("sha1: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true sha1: da39a3ee5e6b4b0d3255bfef95601890afd80709 --- no_error_log [error] lua-resty-string-0.09/t/sha224.t000066400000000000000000000067561232032154500163410ustar00rootroot00000000000000# vi:ft= use Test::Nginx::Socket::Lua; repeat_each(2); plan tests => repeat_each() * (3 * blocks()); our $HttpConfig = <<'_EOC_'; #lua_code_cache off; lua_package_path 'lib/?.lua;;'; lua_package_cpath 'lib/?.so;;'; _EOC_ no_long_string(); run_tests(); __DATA__ === TEST 1: hello SHA-224 --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha224 = require "resty.sha224" local str = require "resty.string" local sha224 = resty_sha224:new() ngx.say(sha224:update("hello")) local digest = sha224:final() ngx.say("sha224: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true sha224: ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193 --- no_error_log [error] === TEST 2: SHA-224 incremental --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha224 = require "resty.sha224" local str = require "resty.string" local sha224 = resty_sha224:new() ngx.say(sha224:update("hel")) ngx.say(sha224:update("lo")) local digest = sha224:final() ngx.say("sha224: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true sha224: ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193 --- no_error_log [error] === TEST 3: SHA-224 empty string --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha224 = require "resty.sha224" local str = require "resty.string" local sha224 = resty_sha224:new() ngx.say(sha224:update("")) local digest = sha224:final() ngx.say("sha224: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true sha224: d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f --- no_error_log [error] === TEST 4: hello (SHA-1 + SHA-224 + SHA-256 + SHA-512 at the same time) --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha224 = require "resty.sha224" local resty_sha256 = require "resty.sha256" local resty_sha1 = require "resty.sha1" local resty_sha512 = require "resty.sha512" local str = require "resty.string" local sha224 = resty_sha224:new() local sha256 = resty_sha256:new() local sha1 = resty_sha1:new() local sha512 = resty_sha512:new() ngx.say(sha224:update("hello")) ngx.say(sha256:update("hello")) ngx.say(sha1:update("hello")) ngx.say(sha512:update("hello")) local digest = sha224:final() ngx.say("sha224: ", str.to_hex(digest)) digest = sha256:final() ngx.say("sha256: ", str.to_hex(digest)) digest = sha1:final() ngx.say("sha1: ", str.to_hex(digest)) digest = sha512:final() ngx.say("sha512: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true true true sha224: ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193 sha256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 sha1: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d sha512: 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043 --- no_error_log [error] lua-resty-string-0.09/t/sha256.t000066400000000000000000000037441232032154500163400ustar00rootroot00000000000000# vi:ft= use Test::Nginx::Socket::Lua; repeat_each(2); plan tests => repeat_each() * (3 * blocks()); our $HttpConfig = <<'_EOC_'; #lua_code_cache off; lua_package_path 'lib/?.lua;;'; lua_package_cpath 'lib/?.so;;'; _EOC_ no_long_string(); run_tests(); __DATA__ === TEST 1: hello SHA-256 --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha256 = require "resty.sha256" local str = require "resty.string" local sha256 = resty_sha256:new() ngx.say(sha256:update("hello")) local digest = sha256:final() ngx.say("sha256: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true sha256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 --- no_error_log [error] === TEST 2: SHA-256 incremental --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha256 = require "resty.sha256" local str = require "resty.string" local sha256 = resty_sha256:new() ngx.say(sha256:update("hel")) ngx.say(sha256:update("lo")) local digest = sha256:final() ngx.say("sha256: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true sha256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 --- no_error_log [error] === TEST 3: SHA-256 empty string --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha256 = require "resty.sha256" local str = require "resty.string" local sha256 = resty_sha256:new() ngx.say(sha256:update("")) local digest = sha256:final() ngx.say("sha256: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 --- no_error_log [error] lua-resty-string-0.09/t/sha384.lua000066400000000000000000000040771232032154500166600ustar00rootroot00000000000000# vi:ft= use Test::Nginx::Socket; repeat_each(2); plan tests => repeat_each() * (3 * blocks()); our $HttpConfig = <<'_EOC_'; #lua_code_cache off; lua_package_path 'lib/?.lua;;'; lua_package_cpath 'lib/?.so;;'; _EOC_ no_long_string(); run_tests(); __DATA__ === TEST 1: hello SHA-384 --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha384 = require "resty.sha384" local str = require "resty.string" local sha384 = resty_sha384:new() ngx.say(sha384:update("hello")) local digest = sha384:final() ngx.say("sha384: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true sha384: 59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f --- no_error_log [error] === TEST 2: SHA-384 incremental --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha384 = require "resty.sha384" local str = require "resty.string" local sha384 = resty_sha384:new() ngx.say(sha384:update("hel")) ngx.say(sha384:update("lo")) local digest = sha384:final() ngx.say("sha384: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true sha384: 59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f --- no_error_log [error] === TEST 3: SHA-384 empty string --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha384 = require "resty.sha384" local str = require "resty.string" local sha384 = resty_sha384:new() ngx.say(sha384:update("")) local digest = sha384:final() ngx.say("sha384: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true sha384: 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b --- no_error_log [error] lua-resty-string-0.09/t/sha512.t000066400000000000000000000042441232032154500163270ustar00rootroot00000000000000# vi:ft= use Test::Nginx::Socket::Lua; repeat_each(2); plan tests => repeat_each() * (3 * blocks()); our $HttpConfig = <<'_EOC_'; #lua_code_cache off; lua_package_path 'lib/?.lua;;'; lua_package_cpath 'lib/?.so;;'; _EOC_ no_long_string(); run_tests(); __DATA__ === TEST 1: hello SHA-512 --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha512 = require "resty.sha512" local str = require "resty.string" local sha512 = resty_sha512:new() ngx.say(sha512:update("hello")) local digest = sha512:final() ngx.say("sha512: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true sha512: 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043 --- no_error_log [error] === TEST 2: SHA-512 incremental --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha512 = require "resty.sha512" local str = require "resty.string" local sha512 = resty_sha512:new() ngx.say(sha512:update("hel")) ngx.say(sha512:update("lo")) local digest = sha512:final() ngx.say("sha512: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true true sha512: 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043 --- no_error_log [error] === TEST 3: SHA-512 empty string --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local resty_sha512 = require "resty.sha512" local str = require "resty.string" local sha512 = resty_sha512:new() ngx.say(sha512:update("")) local digest = sha512:final() ngx.say("sha512: ", str.to_hex(digest)) '; } --- request GET /t --- response_body true sha512: cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e --- no_error_log [error] lua-resty-string-0.09/t/version.t000066400000000000000000000034721232032154500170130ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket::Lua; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks()); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; no_long_string(); #no_diff(); run_tests(); __DATA__ === TEST 1: sha1 version --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local sha1 = require "resty.sha1" ngx.say(sha1._VERSION) '; } --- request GET /t --- response_body_like chop ^\d+\.\d+$ --- no_error_log [error] === TEST 2: md5 version --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local md5 = require "resty.md5" ngx.say(md5._VERSION) '; } --- request GET /t --- response_body_like chop ^\d+\.\d+$ --- no_error_log [error] === TEST 3: resty.string version --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local str = require "resty.string" ngx.say(str._VERSION) '; } --- request GET /t --- response_body_like chop ^\d+\.\d+$ --- no_error_log [error] === TEST 4: resty.random version --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local rand = require "resty.random" ngx.say(rand._VERSION) '; } --- request GET /t --- response_body_like chop ^\d+\.\d+$ --- no_error_log [error] === TEST 5: resty.aes version --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local aes = require "resty.aes" ngx.say(aes._VERSION) '; } --- request GET /t --- response_body_like chop ^\d+\.\d+$ --- no_error_log [error] lua-resty-string-0.09/valgrind.suppress000066400000000000000000000236451232032154500203160ustar00rootroot00000000000000{ Memcheck:Cond fun:lj_str_new obj:* } { Memcheck:Param write(buf) fun:__write_nocancel fun:ngx_log_error_core fun:ngx_resolver_read_response } { Memcheck:Cond fun:ngx_sprintf_num fun:ngx_vslprintf fun:ngx_log_error_core fun:ngx_resolver_read_response fun:ngx_epoll_process_events fun:ngx_process_events_and_timers fun:ngx_single_process_cycle fun:main } { Memcheck:Addr1 fun:ngx_vslprintf fun:ngx_snprintf fun:ngx_sock_ntop fun:ngx_event_accept } { Memcheck:Param write(buf) fun:__write_nocancel fun:ngx_log_error_core fun:ngx_resolver_read_response fun:ngx_event_process_posted fun:ngx_process_events_and_timers fun:ngx_single_process_cycle fun:main } { Memcheck:Cond fun:ngx_sprintf_num fun:ngx_vslprintf fun:ngx_log_error_core fun:ngx_resolver_read_response fun:ngx_event_process_posted fun:ngx_process_events_and_timers fun:ngx_single_process_cycle fun:main } { exp-sgcheck:SorG fun:lj_str_new fun:lua_pushlstring } { Memcheck:Leak fun:malloc fun:ngx_alloc obj:* } { exp-sgcheck:SorG fun:lj_str_new fun:lua_pushlstring } { exp-sgcheck:SorG fun:ngx_http_lua_ndk_set_var_get } { exp-sgcheck:SorG fun:lj_str_new fun:lua_getfield } { exp-sgcheck:SorG fun:lj_str_new fun:lua_setfield } { exp-sgcheck:SorG fun:ngx_http_variables_init_vars fun:ngx_http_block } { exp-sgcheck:SorG fun:ngx_conf_parse } { exp-sgcheck:SorG fun:ngx_vslprintf fun:ngx_log_error_core } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_calloc fun:ngx_event_process_init } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_malloc fun:ngx_pcalloc } { Memcheck:Addr4 fun:lj_str_new fun:lua_setfield } { Memcheck:Addr4 fun:lj_str_new fun:lua_getfield } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:(below main) } { Memcheck:Param epoll_ctl(event) fun:epoll_ctl } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_event_process_init } { Memcheck:Cond fun:ngx_conf_flush_files fun:ngx_single_process_cycle } { Memcheck:Cond fun:memcpy fun:ngx_vslprintf fun:ngx_log_error_core fun:ngx_http_charset_header_filter } { Memcheck:Leak fun:memalign fun:posix_memalign fun:ngx_memalign fun:ngx_pcalloc } { Memcheck:Addr4 fun:lj_str_new fun:lua_pushlstring } { Memcheck:Cond fun:lj_str_new fun:lj_str_fromnum } { Memcheck:Cond fun:lj_str_new fun:lua_pushlstring } { Memcheck:Addr4 fun:lj_str_new fun:lua_setfield fun:ngx_http_lua_cache_store_code } { Memcheck:Cond fun:lj_str_new fun:lua_getfield fun:ngx_http_lua_cache_load_code } { Memcheck:Cond fun:lj_str_new fun:lua_setfield fun:ngx_http_lua_cache_store_code } { Memcheck:Addr4 fun:lj_str_new fun:lua_getfield fun:ngx_http_lua_cache_load_code } { Memcheck:Param socketcall.setsockopt(optval) fun:setsockopt fun:drizzle_state_connect } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_pool_cleanup_add } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_pnalloc } { Memcheck:Cond fun:ngx_conf_flush_files fun:ngx_single_process_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_pcalloc } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_malloc fun:ngx_palloc_large } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_create_pool } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_malloc fun:ngx_palloc } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_malloc fun:ngx_pnalloc } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_array_push fun:ngx_http_get_variable_index fun:ngx_http_memc_add_variable fun:ngx_http_memc_init fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_event_process_init fun:ngx_single_process_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_crc32_table_init fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_event_process_init fun:ngx_worker_process_init fun:ngx_worker_process_cycle fun:ngx_spawn_process fun:ngx_start_worker_processes fun:ngx_master_process_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_hash_init fun:ngx_http_variables_init_vars fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_http_upstream_drizzle_create_srv_conf fun:ngx_http_upstream fun:ngx_conf_parse fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_hash_keys_array_init fun:ngx_http_variables_add_core_vars fun:ngx_http_core_preconfiguration fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_array_push fun:ngx_hash_add_key fun:ngx_http_add_variable fun:ngx_http_echo_add_variables fun:ngx_http_echo_handler_init fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_http_upstream_drizzle_create_srv_conf fun:ngx_http_core_server fun:ngx_conf_parse fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_http_upstream_drizzle_create_srv_conf fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_array_push fun:ngx_hash_add_key fun:ngx_http_variables_add_core_vars fun:ngx_http_core_preconfiguration fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_hash_init fun:ngx_http_upstream_init_main_conf fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_http_drizzle_keepalive_init fun:ngx_http_upstream_drizzle_init fun:ngx_http_upstream_init_main_conf fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_hash_init fun:ngx_http_variables_init_vars fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:memalign fun:posix_memalign fun:ngx_memalign fun:ngx_create_pool } { Memcheck:Leak fun:memalign fun:posix_memalign fun:ngx_memalign fun:ngx_palloc_block fun:ngx_palloc } { Memcheck:Cond fun:index fun:expand_dynamic_string_token fun:_dl_map_object fun:map_doit fun:_dl_catch_error fun:do_preload fun:dl_main fun:_dl_sysdep_start fun:_dl_start }