crypto: return this in setAuthTag/setAAD

Allow method chaining as with setAutoPadding and other methods.

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Kirill Fomichev 2016-11-01 15:24:31 +03:00 committed by Anna Henningsen
parent 55cb280265
commit 6b86ecc007
No known key found for this signature in database
GPG Key ID: D8B9F5AEAE84E4CF
3 changed files with 23 additions and 0 deletions

View File

@ -194,6 +194,8 @@ When using an authenticated encryption mode (only `GCM` is currently
supported), the `cipher.setAAD()` method sets the value used for the supported), the `cipher.setAAD()` method sets the value used for the
_additional authenticated data_ (AAD) input parameter. _additional authenticated data_ (AAD) input parameter.
Returns `this` for method chaining.
### cipher.getAuthTag() ### cipher.getAuthTag()
<!-- YAML <!-- YAML
added: v1.0.0 added: v1.0.0
@ -222,6 +224,8 @@ using `0x0` instead of PKCS padding.
The `cipher.setAutoPadding()` method must be called before [`cipher.final()`][]. The `cipher.setAutoPadding()` method must be called before [`cipher.final()`][].
Returns `this` for method chaining.
### cipher.update(data[, input_encoding][, output_encoding]) ### cipher.update(data[, input_encoding][, output_encoding])
<!-- YAML <!-- YAML
added: v0.1.94 added: v0.1.94
@ -329,6 +333,8 @@ When using an authenticated encryption mode (only `GCM` is currently
supported), the `cipher.setAAD()` method sets the value used for the supported), the `cipher.setAAD()` method sets the value used for the
_additional authenticated data_ (AAD) input parameter. _additional authenticated data_ (AAD) input parameter.
Returns `this` for method chaining.
### decipher.setAuthTag(buffer) ### decipher.setAuthTag(buffer)
<!-- YAML <!-- YAML
added: v1.0.0 added: v1.0.0
@ -340,6 +346,8 @@ received _authentication tag_. If no tag is provided, or if the cipher text
has been tampered with, [`decipher.final()`][] with throw, indicating that the has been tampered with, [`decipher.final()`][] with throw, indicating that the
cipher text should be discarded due to failed authentication. cipher text should be discarded due to failed authentication.
Returns `this` for method chaining.
### decipher.setAutoPadding(auto_padding=true) ### decipher.setAutoPadding(auto_padding=true)
<!-- YAML <!-- YAML
added: v0.7.1 added: v0.7.1
@ -355,6 +363,8 @@ multiple of the ciphers block size.
The `decipher.setAutoPadding()` method must be called before The `decipher.setAutoPadding()` method must be called before
[`decipher.update()`][]. [`decipher.update()`][].
Returns `this` for method chaining.
### decipher.update(data[, input_encoding][, output_encoding]) ### decipher.update(data[, input_encoding][, output_encoding])
<!-- YAML <!-- YAML
added: v0.1.94 added: v0.1.94

View File

@ -177,10 +177,12 @@ Cipher.prototype.getAuthTag = function getAuthTag() {
Cipher.prototype.setAuthTag = function setAuthTag(tagbuf) { Cipher.prototype.setAuthTag = function setAuthTag(tagbuf) {
this._handle.setAuthTag(tagbuf); this._handle.setAuthTag(tagbuf);
return this;
}; };
Cipher.prototype.setAAD = function setAAD(aadbuf) { Cipher.prototype.setAAD = function setAAD(aadbuf) {
this._handle.setAAD(aadbuf); this._handle.setAAD(aadbuf);
return this;
}; };
exports.createCipheriv = exports.Cipheriv = Cipheriv; exports.createCipheriv = exports.Cipheriv = Cipheriv;

View File

@ -139,3 +139,14 @@ testCipher2(Buffer.from('0123456789abcdef'));
assert.doesNotThrow(() => txt += decipher.final('utf-16le')); assert.doesNotThrow(() => txt += decipher.final('utf-16le'));
assert.strictEqual(txt, plaintext, 'decrypted result in utf-16le'); assert.strictEqual(txt, plaintext, 'decrypted result in utf-16le');
} }
// setAutoPadding/setAuthTag/setAAD should return `this`
{
const key = '0123456789';
const tagbuf = Buffer.from('tagbuf');
const aadbuf = Buffer.from('aadbuf');
const decipher = crypto.createDecipher('aes-256-gcm', key);
assert.strictEqual(decipher.setAutoPadding(), decipher);
assert.strictEqual(decipher.setAuthTag(tagbuf), decipher);
assert.strictEqual(decipher.setAAD(aadbuf), decipher);
}