diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 202f49910cdaa8..4770e0c36c3948 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -848,6 +848,9 @@ added: v0.1.94 If `outputEncoding` is specified, a string is returned. If an `outputEncoding` is not provided, a [`Buffer`][] is returned. +If an output encoding was specified in a previous call to +[`cipher.update()`][], `outputEncoding` must use the same encoding. + Once the `cipher.final()` method has been called, the `Cipheriv` object can no longer be used to encrypt data. Attempts to call `cipher.final()` more than once will result in an error being thrown. @@ -940,6 +943,8 @@ The `outputEncoding` specifies the output format of the enciphered data. If the `outputEncoding` is specified, a string using the specified encoding is returned. If no `outputEncoding` is provided, a [`Buffer`][] is returned. +When `outputEncoding` is specified, it must use the same encoding as previous +calls to `cipher.update()`. The `cipher.update()` method can be called multiple times with new data until [`cipher.final()`][] is called. Calling `cipher.update()` after @@ -1158,6 +1163,9 @@ added: v0.1.94 If `outputEncoding` is specified, a string is returned. If an `outputEncoding` is not provided, a [`Buffer`][] is returned. +If an output encoding was specified in a previous call to +[`decipher.update()`][], `outputEncoding` must use the same encoding. + Once the `decipher.final()` method has been called, the `Decipheriv` object can no longer be used to decrypt data. Attempts to call `decipher.final()` more than once will result in an error being thrown. @@ -1290,6 +1298,8 @@ The `outputEncoding` specifies the output format of the enciphered data. If the `outputEncoding` is specified, a string using the specified encoding is returned. If no `outputEncoding` is provided, a [`Buffer`][] is returned. +When `outputEncoding` is specified, it must use the same encoding as previous +calls to `decipher.update()`. The `decipher.update()` method can be called multiple times with new data until [`decipher.final()`][] is called. Calling `decipher.update()` after diff --git a/lib/internal/crypto/cipher.js b/lib/internal/crypto/cipher.js index 61728364986898..5dc2cf95962405 100644 --- a/lib/internal/crypto/cipher.js +++ b/lib/internal/crypto/cipher.js @@ -54,8 +54,6 @@ const { isArrayBufferView, } = require('internal/util/types'); -const assert = require('internal/assert'); - const LazyTransform = require('internal/streams/lazy_transform'); const { normalizeEncoding, kEmptyObject } = require('internal/util'); @@ -98,7 +96,11 @@ function getDecoder(decoder, encoding) { if (normalizedEncoding === undefined) { throw new ERR_UNKNOWN_ENCODING(encoding); } - assert.fail('Cannot change encoding'); + throw new ERR_INVALID_ARG_VALUE( + 'outputEncoding', + encoding, + `cannot be changed from '${decoder.encoding}'`, + ); } return decoder; } diff --git a/test/parallel/test-crypto-encoding-validation-error.js b/test/parallel/test-crypto-encoding-validation-error.js index 0e921ac2862f49..673527a2ad3e31 100644 --- a/test/parallel/test-crypto-encoding-validation-error.js +++ b/test/parallel/test-crypto-encoding-validation-error.js @@ -12,13 +12,19 @@ const createCipher = () => { return createCipheriv('aes-256-cbc', randomBytes(32), randomBytes(16)); }; +const encodingChangeError = { + code: 'ERR_INVALID_ARG_VALUE', + name: 'TypeError', + message: /cannot be changed from 'utf8'/, +}; + { const cipher = createCipher(); cipher.update('test', 'utf-8', 'utf-8'); assert.throws( () => cipher.update('666f6f', 'hex', 'hex'), - { message: /Cannot change encoding/ } + encodingChangeError ); } @@ -28,7 +34,7 @@ const createCipher = () => { assert.throws( () => cipher.final('hex'), - { message: /Cannot change encoding/ } + encodingChangeError ); }