Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/internal/crypto/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-crypto-encoding-validation-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand All @@ -28,7 +34,7 @@ const createCipher = () => {

assert.throws(
() => cipher.final('hex'),
{ message: /Cannot change encoding/ }
encodingChangeError
);
}

Expand Down
Loading