Skip to content

Commit 3e68167

Browse files
committed
fix(*) always return ok, err if there's no explict return value
1 parent 11ea9ae commit 3e68167

File tree

6 files changed

+24
-22
lines changed

6 files changed

+24
-22
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ This function is a shorthand of `cipher:init` plus `cipher:final`.
399399

400400
### cipher:init
401401

402-
**syntax**: *err = cipher:init(key, iv?, opts?)*
402+
**syntax**: *ok, err = cipher:init(key, iv?, opts?)*
403403

404404
Initialize the cipher with key `key` and IV `iv`. The optional third argument is a table consists of:
405405

@@ -471,7 +471,7 @@ Module to interact with message digest (EVP_MD_CTX).
471471
Creates a digest instance. `digest_name` is a case-insensitive string of digest algorithm name.
472472
To view a list of digest algorithms implemented, use `openssl list -digest-algorithms`.
473473

474-
If `digest_name` is omitted, it's by default to `sha1`.
474+
If `digest_name` is omitted, it's default to `sha1`.
475475

476476
### digest.istype
477477

@@ -481,7 +481,7 @@ Returns `true` if table is an instance of `digest`. Returns `false` otherwise.
481481

482482
### digest:update
483483

484-
**syntax**: *err = digest:update(partial, ...)*
484+
**syntax**: *ok, err = digest:update(partial, ...)*
485485

486486
Updates the digest with one or more strings.
487487

@@ -515,7 +515,7 @@ Module to interact with hash-based message authentication code (HMAC_CTX).
515515
Creates a hmac instance. `digest_name` is a case-insensitive string of digest algorithm name.
516516
To view a list of digest algorithms implemented, use `openssl list -digest-algorithms`.
517517

518-
If `digest_name` is omitted, it's by default to `sha1`.
518+
If `digest_name` is omitted, it's default to `sha1`.
519519

520520
### hmac.istype
521521

@@ -525,7 +525,7 @@ Returns `true` if table is an instance of `hmac`. Returns `false` otherwise.
525525

526526
### hmac:update
527527

528-
**syntax**: *err = hmac:update(partial, ...)*
528+
**syntax**: *ok, err = hmac:update(partial, ...)*
529529

530530
Updates the HMAC with one or more strings.
531531

@@ -603,7 +603,7 @@ Returns a digest of the DER representation of the X509 certificate object in raw
603603
`digest_name` is a case-insensitive string of digest algorithm name.
604604
To view a list of digest algorithms implemented, use `openssl list -digest-algorithms`.
605605

606-
If `digest_name` is omitted, it's by default to `sha1`.
606+
If `digest_name` is omitted, it's default to `sha1`.
607607

608608
### x509:pubkey_digest
609609

@@ -614,7 +614,7 @@ Returns a digest of the DER representation of the pubkey in the X509 object in r
614614
`digest_name` is a case-insensitive string of digest algorithm name.
615615
To view a list of digest algorithms implemented, use `openssl list -digest-algorithms`.
616616

617-
If `digest_name` is omitted, it's by default to `sha1`.
617+
If `digest_name` is omitted, it's default to `sha1`.
618618

619619
### x509:get_*, x509:set_*
620620

lib/resty/openssl/cipher.lua

+7-5
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ end
5858
function _M:init(key, iv, opts)
5959
opts = opts or {}
6060
if not key or #key ~= self.key_size then
61-
return string.format("incorrect key size, expect %d", self.key_size)
61+
return false, string.format("incorrect key size, expect %d", self.key_size)
6262
end
6363
if not iv or #iv ~= self.iv_size then
64-
return string.format("incorrect iv size, expect %d", self.iv_size)
64+
return false, string.format("incorrect iv size, expect %d", self.iv_size)
6565
end
6666

6767
if C.EVP_CipherInit_ex(self.ctx, nil, nil, key, iv, opts.is_encrypt and 1 or 0) == 0 then
68-
return format_error("cipher:init")
68+
return false, format_error("cipher:init")
6969
end
7070

7171
if opts.no_padding then
@@ -74,10 +74,12 @@ function _M:init(key, iv, opts)
7474
end
7575

7676
self.initialized = true
77+
78+
return true
7779
end
7880

7981
function _M:encrypt(key, iv, s, no_padding)
80-
local err = self:init(key, iv, {
82+
local _, err = self:init(key, iv, {
8183
is_encrypt = true,
8284
no_padding = no_padding,
8385
})
@@ -88,7 +90,7 @@ function _M:encrypt(key, iv, s, no_padding)
8890
end
8991

9092
function _M:decrypt(key, iv, s, no_padding)
91-
local err = self:init(key, iv, {
93+
local _, err = self:init(key, iv, {
9294
is_encrypt = false,
9395
no_padding = no_padding,
9496
})

lib/resty/openssl/digest.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ end
5050
function _M:update(...)
5151
for _, s in ipairs({...}) do
5252
if C.EVP_DigestUpdate(self.ctx, s, #s) ~= 1 then
53-
return format_error("digest:update")
53+
return false, format_error("digest:update")
5454
end
5555
end
56-
return nil
56+
return true, nil
5757
end
5858

5959
local uint_ptr = ffi.typeof("unsigned int[1]")
6060

6161
function _M:final(s)
6262
if s then
63-
local err = self:update(s)
63+
local _, err = self:update(s)
6464
if err then
6565
return nil, err
6666
end

lib/resty/openssl/hmac.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ end
5252
function _M:update(...)
5353
for _, s in ipairs({...}) do
5454
if C.HMAC_Update(self.ctx, s, #s) ~= 1 then
55-
return format_error("hmac:update")
55+
return false, format_error("hmac:update")
5656
end
5757
end
58-
return nil
58+
return true, nil
5959
end
6060

6161
local uint_ptr = ffi.typeof("unsigned int[1]")
6262

6363
function _M:final(s)
6464
if s then
65-
local err = self:update(s)
65+
local _, err = self:update(s)
6666
if err then
6767
return nil, err
6868
end

t/openssl/cipher.t

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ __DATA__
2323
ngx.log(ngx.ERR, err)
2424
return
2525
end
26-
err = cipher:init(string.rep("0", 32), string.rep("0", 16), {
26+
local ok, err = cipher:init(string.rep("0", 32), string.rep("0", 16), {
2727
is_encrypt = true,
2828
})
2929
if err then
@@ -206,7 +206,7 @@ VhGyRCcMvlAgUjTYrqiWpg=="
206206
ngx.log(ngx.ERR, err)
207207
return
208208
end
209-
local err = cipher:init(string.rep("0", 32), string.rep("0", 16), {
209+
local ok, err = cipher:init(string.rep("0", 32), string.rep("0", 16), {
210210
is_encrypt = true,
211211
})
212212
if err then
@@ -258,7 +258,7 @@ yP4vKOecDyao4AzxaTAzkA==
258258
ngx.log(ngx.ERR, err)
259259
return
260260
end
261-
local err = cipher:init(string.rep("0", 32), string.rep("0", 16), {
261+
local ok, err = cipher:init(string.rep("0", 32), string.rep("0", 16), {
262262
is_encrypt = false,
263263
})
264264
if err then

t/openssl/pkey.t

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ AQAB
196196
ngx.log(ngx.ERR, err)
197197
return
198198
end
199-
err = digest:update("🕶️", "+1s")
199+
local _, err = digest:update("🕶️", "+1s")
200200
if err then
201201
ngx.log(ngx.ERR, err)
202202
return

0 commit comments

Comments
 (0)