Skip to content

Commit 875674b

Browse files
fanatidMylesBorins
authored andcommitted
crypto: return this in setAuthTag/setAAD
Allow method chaining as with setAutoPadding and other methods. PR-URL: #9398 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sam Roberts <sam@strongloop.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f8da60f commit 875674b

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/api/crypto.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ When using an authenticated encryption mode (only `GCM` is currently
194194
supported), the `cipher.setAAD()` method sets the value used for the
195195
_additional authenticated data_ (AAD) input parameter.
196196

197+
Returns `this` for method chaining.
198+
197199
### cipher.getAuthTag()
198200
<!-- YAML
199201
added: v1.0.0
@@ -222,6 +224,8 @@ using `0x0` instead of PKCS padding.
222224

223225
The `cipher.setAutoPadding()` method must be called before [`cipher.final()`][].
224226

227+
Returns `this` for method chaining.
228+
225229
### cipher.update(data[, input_encoding][, output_encoding])
226230
<!-- YAML
227231
added: v0.1.94
@@ -329,6 +333,8 @@ When using an authenticated encryption mode (only `GCM` is currently
329333
supported), the `decipher.setAAD()` method sets the value used for the
330334
_additional authenticated data_ (AAD) input parameter.
331335

336+
Returns `this` for method chaining.
337+
332338
### decipher.setAuthTag(buffer)
333339
<!-- YAML
334340
added: v1.0.0
@@ -340,6 +346,8 @@ received _authentication tag_. If no tag is provided, or if the cipher text
340346
has been tampered with, [`decipher.final()`][] with throw, indicating that the
341347
cipher text should be discarded due to failed authentication.
342348

349+
Returns `this` for method chaining.
350+
343351
### decipher.setAutoPadding(auto_padding=true)
344352
<!-- YAML
345353
added: v0.7.1
@@ -355,6 +363,8 @@ multiple of the ciphers block size.
355363
The `decipher.setAutoPadding()` method must be called before
356364
[`decipher.update()`][].
357365

366+
Returns `this` for method chaining.
367+
358368
### decipher.update(data[, input_encoding][, output_encoding])
359369
<!-- YAML
360370
added: v0.1.94

lib/crypto.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,12 @@ Cipher.prototype.getAuthTag = function() {
178178

179179
Cipher.prototype.setAuthTag = function(tagbuf) {
180180
this._handle.setAuthTag(tagbuf);
181+
return this;
181182
};
182183

183184
Cipher.prototype.setAAD = function(aadbuf) {
184185
this._handle.setAAD(aadbuf);
186+
return this;
185187
};
186188

187189
exports.createCipheriv = exports.Cipheriv = Cipheriv;

test/parallel/test-crypto-cipher-decipher.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,14 @@ testCipher2(Buffer.from('0123456789abcdef'));
139139
assert.doesNotThrow(() => txt += decipher.final('utf-16le'));
140140
assert.strictEqual(txt, plaintext, 'decrypted result in utf-16le');
141141
}
142+
143+
// setAutoPadding/setAuthTag/setAAD should return `this`
144+
{
145+
const key = '0123456789';
146+
const tagbuf = Buffer.from('tagbuf');
147+
const aadbuf = Buffer.from('aadbuf');
148+
const decipher = crypto.createDecipher('aes-256-gcm', key);
149+
assert.strictEqual(decipher.setAutoPadding(), decipher);
150+
assert.strictEqual(decipher.setAuthTag(tagbuf), decipher);
151+
assert.strictEqual(decipher.setAAD(aadbuf), decipher);
152+
}

0 commit comments

Comments
 (0)