Skip to content

Commit 389969e

Browse files
addaleaxTrott
authored andcommittedOct 3, 2019
crypto: remove arbitrary UTF16 restriction
Since 71f633a, this is no longer necessary. Refs: #22622 Fixes: #29793 PR-URL: #29795 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent 500720f commit 389969e

File tree

6 files changed

+18
-30
lines changed

6 files changed

+18
-30
lines changed
 

‎doc/api/errors.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -765,14 +765,6 @@ to enable or disable FIPS mode in the `crypto` module.
765765
An attempt was made to enable or disable FIPS mode, but FIPS mode was not
766766
available.
767767

768-
<a id="ERR_CRYPTO_HASH_DIGEST_NO_UTF16"></a>
769-
### ERR_CRYPTO_HASH_DIGEST_NO_UTF16
770-
771-
The UTF-16 encoding was used with [`hash.digest()`][]. While the
772-
`hash.digest()` method does allow an `encoding` argument to be passed in,
773-
causing the method to return a string rather than a `Buffer`, the UTF-16
774-
encoding (e.g. `ucs` or `utf16le`) is not supported.
775-
776768
<a id="ERR_CRYPTO_HASH_FINALIZED"></a>
777769
### ERR_CRYPTO_HASH_FINALIZED
778770

@@ -2078,6 +2070,18 @@ removed: v11.12.0
20782070
There was an attempt to use a `MessagePort` instance in a closed
20792071
state, usually after `.close()` has been called.
20802072

2073+
<a id="ERR_CRYPTO_HASH_DIGEST_NO_UTF16"></a>
2074+
### ERR_CRYPTO_HASH_DIGEST_NO_UTF16
2075+
<!-- YAML
2076+
added: v9.0.0
2077+
removed: REPLACEME
2078+
-->
2079+
2080+
The UTF-16 encoding was used with [`hash.digest()`][]. While the
2081+
`hash.digest()` method does allow an `encoding` argument to be passed in,
2082+
causing the method to return a string rather than a `Buffer`, the UTF-16
2083+
encoding (e.g. `ucs` or `utf16le`) is not supported.
2084+
20812085
<a id="ERR_HTTP2_FRAME_ERROR"></a>
20822086
### ERR_HTTP2_FRAME_ERROR
20832087
<!-- YAML

‎lib/internal/crypto/hash.js

-6
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ const {
2020
const { Buffer } = require('buffer');
2121

2222
const {
23-
ERR_CRYPTO_HASH_DIGEST_NO_UTF16,
2423
ERR_CRYPTO_HASH_FINALIZED,
2524
ERR_CRYPTO_HASH_UPDATE_FAILED,
2625
ERR_INVALID_ARG_TYPE
2726
} = require('internal/errors').codes;
2827
const { validateEncoding, validateString, validateUint32 } =
2928
require('internal/validators');
30-
const { normalizeEncoding } = require('internal/util');
3129
const { isArrayBufferView } = require('internal/util/types');
3230
const LazyTransform = require('internal/streams/lazy_transform');
3331
const kState = Symbol('kState');
@@ -90,8 +88,6 @@ Hash.prototype.digest = function digest(outputEncoding) {
9088
if (state[kFinalized])
9189
throw new ERR_CRYPTO_HASH_FINALIZED();
9290
outputEncoding = outputEncoding || getDefaultEncoding();
93-
if (normalizeEncoding(outputEncoding) === 'utf16le')
94-
throw new ERR_CRYPTO_HASH_DIGEST_NO_UTF16();
9591

9692
// Explicit conversion for backward compatibility.
9793
const ret = this[kHandle].digest(`${outputEncoding}`);
@@ -121,8 +117,6 @@ Hmac.prototype.update = Hash.prototype.update;
121117
Hmac.prototype.digest = function digest(outputEncoding) {
122118
const state = this[kState];
123119
outputEncoding = outputEncoding || getDefaultEncoding();
124-
if (normalizeEncoding(outputEncoding) === 'utf16le')
125-
throw new ERR_CRYPTO_HASH_DIGEST_NO_UTF16();
126120

127121
if (state[kFinalized]) {
128122
const buf = Buffer.from('');

‎lib/internal/errors.js

-2
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,6 @@ E('ERR_CRYPTO_FIPS_FORCED',
749749
'Cannot set FIPS mode, it was forced with --force-fips at startup.', Error);
750750
E('ERR_CRYPTO_FIPS_UNAVAILABLE', 'Cannot set FIPS mode in a non-FIPS build.',
751751
Error);
752-
E('ERR_CRYPTO_HASH_DIGEST_NO_UTF16', 'hash.digest() does not support UTF-16',
753-
Error);
754752
E('ERR_CRYPTO_HASH_FINALIZED', 'Digest already called', Error);
755753
E('ERR_CRYPTO_HASH_UPDATE_FAILED', 'Hash update failed', Error);
756754
E('ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS', 'The selected key encoding %s %s.',

‎src/node_crypto.cc

-1
Original file line numberDiff line numberDiff line change
@@ -4678,7 +4678,6 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
46784678
if (args.Length() >= 1) {
46794679
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
46804680
}
4681-
CHECK_NE(encoding, UCS2); // Digest does not support UTF-16
46824681

46834682
unsigned char md_value[EVP_MAX_MD_SIZE];
46844683
unsigned int md_len = 0;

‎test/parallel/test-crypto-hash.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,9 @@ common.expectsError(
161161
type: Error
162162
});
163163

164-
common.expectsError(
165-
() => crypto.createHash('sha256').digest('ucs2'),
166-
{
167-
code: 'ERR_CRYPTO_HASH_DIGEST_NO_UTF16',
168-
type: Error
169-
}
170-
);
164+
assert.strictEqual(
165+
crypto.createHash('sha256').update('test').digest('ucs2'),
166+
crypto.createHash('sha256').update('test').digest().toString('ucs2'));
171167

172168
common.expectsError(
173169
() => crypto.createHash(),

‎test/parallel/test-crypto-hmac.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,9 @@ const rfc2202_sha1 = [
408408
for (const { key, data, hmac } of rfc2202_sha1)
409409
testHmac('sha1', key, data, hmac);
410410

411-
common.expectsError(
412-
() => crypto.createHmac('sha256', 'w00t').digest('ucs2'),
413-
{
414-
code: 'ERR_CRYPTO_HASH_DIGEST_NO_UTF16',
415-
type: Error
416-
});
411+
assert.strictEqual(
412+
crypto.createHmac('sha256', 'w00t').digest('ucs2'),
413+
crypto.createHmac('sha256', 'w00t').digest().toString('ucs2'));
417414

418415
// Check initialized -> uninitialized state transition after calling digest().
419416
{

0 commit comments

Comments
 (0)
Please sign in to comment.