Skip to content

Commit 08a7e7b

Browse files
fanatidaddaleax
authored andcommitted
crypto: return this in setAuthTag/setAAD
Allow method chaining as with setAutoPadding and other methods. PR-URL: #9398 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b070df8 commit 08a7e7b

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/api/crypto.md

+10
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 `cipher.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

+2
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ Cipher.prototype.getAuthTag = function getAuthTag() {
177177

178178
Cipher.prototype.setAuthTag = function setAuthTag(tagbuf) {
179179
this._handle.setAuthTag(tagbuf);
180+
return this;
180181
};
181182

182183
Cipher.prototype.setAAD = function setAAD(aadbuf) {
183184
this._handle.setAAD(aadbuf);
185+
return this;
184186
};
185187

186188
exports.createCipheriv = exports.Cipheriv = Cipheriv;

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

+11
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)