Skip to content

Commit dac54bf

Browse files
committed
perf(kdf) use table.nkeys for params
1 parent 3d0a51c commit dac54bf

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local nkeys
2+
3+
do
4+
local pok, perr = pcall(require, "table.nkeys")
5+
if pok then
6+
nkeys = perr
7+
else
8+
nkeys = function(tbl)
9+
local cnt = 0
10+
for _ in pairs(tbl) do
11+
cnt = cnt + 1
12+
end
13+
return cnt
14+
end
15+
end
16+
end
17+
18+
return {
19+
nkeys = nkeys,
20+
}

lib/resty/openssl/kdf.lua

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local format_error = require("resty.openssl.err").format_error
1313
local version_text = require("resty.openssl.version").version_text
1414
local OPENSSL_3X = require("resty.openssl.version").OPENSSL_3X
1515
local ctypes = require "resty.openssl.auxiliary.ctypes"
16+
local nkeys = require "resty.openssl.auxiliary.compat".nkeys
1617

1718
--[[
1819
https://wiki.openssl.org/index.php/EVP_Key_Derivation
@@ -311,6 +312,7 @@ function _M.new(typ, properties)
311312
algo = algo,
312313
buf = buf,
313314
buf_size = buf_size,
315+
schema = nil,
314316
}, mt), nil
315317
end
316318

@@ -344,14 +346,16 @@ function _M:derive(outlen, options, options_count)
344346
if options_count then
345347
options_count = options_count - 1
346348
else
347-
options_count = 0
348-
for k, v in pairs(options) do options_count = options_count + 1 end
349+
options_count = nkeys(options)
349350
end
350351

351352
local param, err
352353
if options_count > 0 then
353-
local schema = self:settable_params(true) -- raw schema
354-
param, err = param_lib.construct(options, nil, schema)
354+
if not self.schema then
355+
self.schema = self:settable_params(true) -- raw schema
356+
end
357+
358+
param, err = param_lib.construct(options, nil, self.schema)
355359
if err then
356360
return nil, "kdf:derive: " .. err
357361
end

lib/resty/openssl/param.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require "resty.openssl.include.param"
88
local format_error = require("resty.openssl.err").format_error
99
local bn_lib = require("resty.openssl.bn")
1010
local null = require("resty.openssl.auxiliary.ctypes").null
11+
local nkeys = require "resty.openssl.auxiliary.compat".nkeys
1112

1213
local OSSL_PARAM_INTEGER = 1
1314
local OSSL_PARAM_UNSIGNED_INTEGER = 2
@@ -22,8 +23,7 @@ local buf_param_key = {}
2223

2324
local function construct(buf_t, length, types_map, types_size)
2425
if not length then
25-
length = 0
26-
for k, v in pairs(buf_t) do length = length + 1 end
26+
length = nkeys(buf_t)
2727
end
2828

2929
local params = ffi_new("OSSL_PARAM[?]", length + 1)

lua-resty-openssl-1.1.0-1.rockspec

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ build = {
1515
["resty.openssl"] = "lib/resty/openssl.lua",
1616
["resty.openssl.asn1"] = "lib/resty/openssl/asn1.lua",
1717
["resty.openssl.auxiliary.bio"] = "lib/resty/openssl/auxiliary/bio.lua",
18+
["resty.openssl.auxiliary.compat"] = "lib/resty/openssl/auxiliary/compat.lua",
1819
["resty.openssl.auxiliary.ctypes"] = "lib/resty/openssl/auxiliary/ctypes.lua",
1920
["resty.openssl.auxiliary.ecdsa"] = "lib/resty/openssl/auxiliary/ecdsa.lua",
2021
["resty.openssl.auxiliary.jwk"] = "lib/resty/openssl/auxiliary/jwk.lua",

0 commit comments

Comments
 (0)