2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-11-26 09:34:43 +01:00
|
|
|
const common = require('../common');
|
2010-05-03 23:37:49 +01:00
|
|
|
|
2017-07-01 02:29:09 +03:00
|
|
|
if (!common.hasCrypto)
|
2016-05-11 15:34:52 -04:00
|
|
|
common.skip('missing crypto');
|
2010-05-03 23:37:49 +01:00
|
|
|
|
2016-11-26 09:34:43 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const tls = require('tls');
|
2017-07-17 15:33:46 -07:00
|
|
|
const fixtures = require('../common/fixtures');
|
2025-01-24 16:58:32 -08:00
|
|
|
const { hasOpenSSL3 } = require('../common/crypto');
|
2012-10-22 10:37:20 -07:00
|
|
|
|
2010-06-18 19:42:58 +10:00
|
|
|
// Test Certificates
|
2019-05-29 11:43:44 -07:00
|
|
|
const certPfx = fixtures.readKey('rsa_cert.pfx');
|
2010-05-03 23:37:49 +01:00
|
|
|
|
2014-03-06 15:44:18 -05:00
|
|
|
// 'this' safety
|
|
|
|
// https://github.com/joyent/node/issues/6690
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2018-11-07 16:18:03 -08:00
|
|
|
const credentials = tls.createSecureContext();
|
2016-11-26 09:34:43 +01:00
|
|
|
const context = credentials.context;
|
2018-11-07 16:18:03 -08:00
|
|
|
const notcontext = { setOptions: context.setOptions };
|
|
|
|
|
|
|
|
// Methods of native objects should not segfault when reassigned to a new
|
|
|
|
// object and called illegally. This core dumped in 0.10 and was fixed in
|
|
|
|
// 0.11.
|
|
|
|
notcontext.setOptions();
|
2017-08-09 22:26:03 -05:00
|
|
|
}, (err) => {
|
|
|
|
// Throws TypeError, so there is no opensslErrorStack property.
|
2019-12-25 18:02:16 +01:00
|
|
|
return err instanceof TypeError &&
|
|
|
|
err.name === 'TypeError' &&
|
|
|
|
/^TypeError: Illegal invocation$/.test(err) &&
|
|
|
|
!('opensslErrorStack' in err);
|
2017-08-09 22:26:03 -05:00
|
|
|
});
|
2014-03-06 15:44:18 -05:00
|
|
|
|
2012-05-14 01:08:23 +05:30
|
|
|
// PFX tests
|
2018-02-09 02:32:04 +01:00
|
|
|
tls.createSecureContext({ pfx: certPfx, passphrase: 'sample' });
|
2012-05-14 01:08:23 +05:30
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2017-07-10 20:55:21 -04:00
|
|
|
tls.createSecureContext({ pfx: certPfx });
|
2017-08-09 22:26:03 -05:00
|
|
|
}, (err) => {
|
|
|
|
// Throws general Error, so there is no opensslErrorStack property.
|
2019-12-25 18:02:16 +01:00
|
|
|
return err instanceof Error &&
|
|
|
|
err.name === 'Error' &&
|
|
|
|
/^Error: mac verify failure$/.test(err) &&
|
|
|
|
!('opensslErrorStack' in err);
|
2017-08-09 22:26:03 -05:00
|
|
|
});
|
2012-05-14 01:08:23 +05:30
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2017-07-10 20:55:21 -04:00
|
|
|
tls.createSecureContext({ pfx: certPfx, passphrase: 'test' });
|
2017-08-09 22:26:03 -05:00
|
|
|
}, (err) => {
|
|
|
|
// Throws general Error, so there is no opensslErrorStack property.
|
2019-12-25 18:02:16 +01:00
|
|
|
return err instanceof Error &&
|
|
|
|
err.name === 'Error' &&
|
|
|
|
/^Error: mac verify failure$/.test(err) &&
|
|
|
|
!('opensslErrorStack' in err);
|
2017-08-09 22:26:03 -05:00
|
|
|
});
|
2010-12-05 22:15:30 +03:00
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2017-07-10 20:55:21 -04:00
|
|
|
tls.createSecureContext({ pfx: 'sample', passphrase: 'test' });
|
2017-08-09 22:26:03 -05:00
|
|
|
}, (err) => {
|
|
|
|
// Throws general Error, so there is no opensslErrorStack property.
|
2019-12-25 18:02:16 +01:00
|
|
|
return err instanceof Error &&
|
|
|
|
err.name === 'Error' &&
|
|
|
|
/^Error: not enough data$/.test(err) &&
|
|
|
|
!('opensslErrorStack' in err);
|
2017-08-09 22:26:03 -05:00
|
|
|
});
|
2012-05-14 01:08:23 +05:30
|
|
|
|
2010-05-03 23:37:49 +01:00
|
|
|
|
2011-03-14 11:16:35 +01:00
|
|
|
// update() should only take buffers / strings
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2017-09-06 08:10:34 -07:00
|
|
|
() => crypto.createHash('sha1').update({ foo: 'bar' }),
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError'
|
2017-09-06 08:10:34 -07:00
|
|
|
});
|
2011-01-19 02:00:38 +01:00
|
|
|
|
2011-06-08 18:20:17 -07:00
|
|
|
|
2017-01-12 22:16:21 -08:00
|
|
|
function validateList(list) {
|
|
|
|
// The list must not be empty
|
|
|
|
assert(list.length > 0);
|
|
|
|
|
|
|
|
// The list should be sorted.
|
2013-11-21 13:23:43 +01:00
|
|
|
// Array#sort() modifies the list in place so make a copy.
|
2017-01-12 22:16:21 -08:00
|
|
|
const sorted = [...list].sort();
|
2016-04-19 15:37:45 -07:00
|
|
|
assert.deepStrictEqual(list, sorted);
|
2017-01-12 22:16:21 -08:00
|
|
|
|
|
|
|
// Each element should be unique.
|
|
|
|
assert.strictEqual([...new Set(list)].length, list.length);
|
|
|
|
|
|
|
|
// Each element should be a string.
|
|
|
|
assert(list.every((value) => typeof value === 'string'));
|
2012-10-13 02:49:53 +02:00
|
|
|
}
|
|
|
|
|
2013-03-19 00:16:55 +01:00
|
|
|
// Assume that we have at least AES-128-CBC.
|
2017-01-12 22:16:21 -08:00
|
|
|
const cryptoCiphers = crypto.getCiphers();
|
2016-11-26 09:34:43 +01:00
|
|
|
assert(crypto.getCiphers().includes('aes-128-cbc'));
|
2017-01-12 22:16:21 -08:00
|
|
|
validateList(cryptoCiphers);
|
2022-02-05 14:42:22 -05:00
|
|
|
// Make sure all of the ciphers are supported by OpenSSL
|
|
|
|
for (const algo of cryptoCiphers) {
|
|
|
|
const { ivLength, keyLength, mode } = crypto.getCipherInfo(algo);
|
|
|
|
let options;
|
|
|
|
if (mode === 'ccm')
|
|
|
|
options = { authTagLength: 8 };
|
|
|
|
else if (mode === 'ocb' || algo === 'chacha20-poly1305')
|
|
|
|
options = { authTagLength: 16 };
|
|
|
|
crypto.createCipheriv(algo,
|
|
|
|
crypto.randomBytes(keyLength),
|
|
|
|
crypto.randomBytes(ivLength || 0),
|
|
|
|
options);
|
|
|
|
}
|
2012-10-13 02:44:11 +02:00
|
|
|
|
2013-03-19 00:16:55 +01:00
|
|
|
// Assume that we have at least AES256-SHA.
|
2017-01-12 22:16:21 -08:00
|
|
|
const tlsCiphers = tls.getCiphers();
|
2016-11-26 09:34:43 +01:00
|
|
|
assert(tls.getCiphers().includes('aes256-sha'));
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-28 17:58:08 -08:00
|
|
|
assert(tls.getCiphers().includes('tls_aes_128_ccm_8_sha256'));
|
2017-01-12 22:16:21 -08:00
|
|
|
// There should be no capital letters in any element.
|
2017-06-18 16:22:32 +03:00
|
|
|
const noCapitals = /^[^A-Z]+$/;
|
|
|
|
assert(tlsCiphers.every((value) => noCapitals.test(value)));
|
2017-01-12 22:16:21 -08:00
|
|
|
validateList(tlsCiphers);
|
2013-03-19 00:16:55 +01:00
|
|
|
|
2017-09-23 00:16:52 -04:00
|
|
|
// Assert that we have sha1 and sha256 but not SHA1 and SHA256.
|
2018-05-14 17:28:26 +02:00
|
|
|
assert.notStrictEqual(crypto.getHashes().length, 0);
|
2016-11-26 09:34:43 +01:00
|
|
|
assert(crypto.getHashes().includes('sha1'));
|
2017-09-23 00:16:52 -04:00
|
|
|
assert(crypto.getHashes().includes('sha256'));
|
2016-11-26 09:34:43 +01:00
|
|
|
assert(!crypto.getHashes().includes('SHA1'));
|
2017-09-23 00:16:52 -04:00
|
|
|
assert(!crypto.getHashes().includes('SHA256'));
|
2016-11-26 09:34:43 +01:00
|
|
|
assert(crypto.getHashes().includes('RSA-SHA1'));
|
|
|
|
assert(!crypto.getHashes().includes('rsa-sha1'));
|
2017-01-12 22:16:21 -08:00
|
|
|
validateList(crypto.getHashes());
|
2022-02-05 14:42:22 -05:00
|
|
|
// Make sure all of the hashes are supported by OpenSSL
|
|
|
|
for (const algo of crypto.getHashes())
|
|
|
|
crypto.createHash(algo);
|
2013-02-11 21:22:34 +01:00
|
|
|
|
2015-06-08 12:26:16 -04:00
|
|
|
// Assume that we have at least secp384r1.
|
2018-05-14 17:28:26 +02:00
|
|
|
assert.notStrictEqual(crypto.getCurves().length, 0);
|
2016-11-26 09:34:43 +01:00
|
|
|
assert(crypto.getCurves().includes('secp384r1'));
|
|
|
|
assert(!crypto.getCurves().includes('SECP384R1'));
|
2017-01-12 22:16:21 -08:00
|
|
|
validateList(crypto.getCurves());
|
2015-06-08 12:26:16 -04:00
|
|
|
|
2017-01-13 14:28:35 -08:00
|
|
|
// Modifying return value from get* functions should not mutate subsequent
|
|
|
|
// return values.
|
|
|
|
function testImmutability(fn) {
|
|
|
|
const list = fn();
|
|
|
|
const copy = [...list];
|
|
|
|
list.push('some-arbitrary-value');
|
|
|
|
assert.deepStrictEqual(fn(), copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
testImmutability(crypto.getCiphers);
|
|
|
|
testImmutability(tls.getCiphers);
|
|
|
|
testImmutability(crypto.getHashes);
|
|
|
|
testImmutability(crypto.getCurves);
|
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
const encodingError = {
|
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
|
|
name: 'TypeError',
|
|
|
|
message: "The argument 'encoding' is invalid for data of length 1." +
|
|
|
|
" Received 'hex'",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.throws(
|
2019-05-11 20:00:38 +03:00
|
|
|
() => crypto.createHash('sha1').update('0', 'hex'),
|
2019-12-25 18:02:16 +01:00
|
|
|
(error) => {
|
|
|
|
assert.ok(!('opensslErrorStack' in error));
|
|
|
|
assert.throws(() => { throw error; }, encodingError);
|
|
|
|
return true;
|
2017-08-09 22:26:03 -05:00
|
|
|
}
|
2019-05-11 20:00:38 +03:00
|
|
|
);
|
2013-07-30 14:27:13 +02:00
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2019-05-11 20:00:38 +03:00
|
|
|
() => crypto.createHmac('sha256', 'a secret').update('0', 'hex'),
|
2019-12-25 18:02:16 +01:00
|
|
|
(error) => {
|
|
|
|
assert.ok(!('opensslErrorStack' in error));
|
|
|
|
assert.throws(() => { throw error; }, encodingError);
|
|
|
|
return true;
|
2017-08-09 22:26:03 -05:00
|
|
|
}
|
2019-05-11 20:00:38 +03:00
|
|
|
);
|
2013-09-28 23:33:29 -07:00
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2016-11-26 09:34:43 +01:00
|
|
|
const priv = [
|
2014-01-26 20:09:14 +04:00
|
|
|
'-----BEGIN RSA PRIVATE KEY-----',
|
|
|
|
'MIGrAgEAAiEA+3z+1QNF2/unumadiwEr+C5vfhezsb3hp4jAnCNRpPcCAwEAAQIgQNriSQK4',
|
|
|
|
'EFwczDhMZp2dvbcz7OUUyt36z3S4usFPHSECEQD/41K7SujrstBfoCPzwC1xAhEA+5kt4BJy',
|
|
|
|
'eKN7LggbF3Dk5wIQN6SL+fQ5H/+7NgARsVBp0QIRANxYRukavs4QvuyNhMx+vrkCEQCbf6j/',
|
|
|
|
'Ig6/HueCK/0Jkmp+',
|
|
|
|
'-----END RSA PRIVATE KEY-----',
|
2021-03-26 08:51:08 -07:00
|
|
|
'',
|
2014-01-26 20:09:14 +04:00
|
|
|
].join('\n');
|
2017-09-09 18:41:56 -04:00
|
|
|
crypto.createSign('SHA256').update('test').sign(priv);
|
2017-08-09 22:26:03 -05:00
|
|
|
}, (err) => {
|
2025-01-24 16:58:32 -08:00
|
|
|
if (!hasOpenSSL3)
|
2020-10-26 07:23:55 +01:00
|
|
|
assert.ok(!('opensslErrorStack' in err));
|
2025-01-24 16:58:32 -08:00
|
|
|
assert.throws(() => { throw err; }, hasOpenSSL3 ? {
|
2020-10-26 07:23:55 +01:00
|
|
|
name: 'Error',
|
|
|
|
message: 'error:02000070:rsa routines::digest too big for rsa key',
|
|
|
|
library: 'rsa routines',
|
|
|
|
} : {
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'Error',
|
|
|
|
message: /routines:RSA_sign:digest too big for rsa key$/,
|
2025-05-22 13:26:56 +02:00
|
|
|
library: /rsa routines/i,
|
2019-12-25 18:02:16 +01:00
|
|
|
function: 'RSA_sign',
|
2025-05-22 13:26:56 +02:00
|
|
|
reason: /digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i,
|
2019-12-25 18:02:16 +01:00
|
|
|
code: 'ERR_OSSL_RSA_DIGEST_TOO_BIG_FOR_RSA_KEY'
|
|
|
|
});
|
|
|
|
return true;
|
2017-08-09 22:26:03 -05:00
|
|
|
});
|
2014-01-27 11:02:59 -08:00
|
|
|
|
2025-01-24 16:58:32 -08:00
|
|
|
if (!hasOpenSSL3) {
|
2020-10-26 07:23:55 +01:00
|
|
|
assert.throws(() => {
|
|
|
|
// The correct header inside `rsa_private_pkcs8_bad.pem` should have been
|
|
|
|
// -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY-----
|
|
|
|
// instead of
|
|
|
|
// -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY-----
|
|
|
|
const sha1_privateKey = fixtures.readKey('rsa_private_pkcs8_bad.pem',
|
|
|
|
'ascii');
|
|
|
|
// This would inject errors onto OpenSSL's error stack
|
|
|
|
crypto.createSign('sha1').sign(sha1_privateKey);
|
|
|
|
}, (err) => {
|
|
|
|
// Do the standard checks, but then do some custom checks afterwards.
|
|
|
|
assert.throws(() => { throw err; }, {
|
|
|
|
message: 'error:0D0680A8:asn1 encoding routines:asn1_check_tlen:' +
|
|
|
|
'wrong tag',
|
|
|
|
library: 'asn1 encoding routines',
|
|
|
|
function: 'asn1_check_tlen',
|
|
|
|
reason: 'wrong tag',
|
|
|
|
code: 'ERR_OSSL_ASN1_WRONG_TAG',
|
|
|
|
});
|
|
|
|
// Throws crypto error, so there is an opensslErrorStack property.
|
|
|
|
// The openSSL stack should have content.
|
|
|
|
assert(Array.isArray(err.opensslErrorStack));
|
|
|
|
assert(err.opensslErrorStack.length > 0);
|
|
|
|
return true;
|
2019-03-27 12:10:21 -07:00
|
|
|
});
|
2020-10-26 07:23:55 +01:00
|
|
|
}
|
2015-08-18 10:32:21 +08:00
|
|
|
|
2013-09-28 23:33:29 -07:00
|
|
|
// Make sure memory isn't released before being returned
|
|
|
|
console.log(crypto.randomBytes(16));
|
2016-10-30 13:22:50 -07:00
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => {
|
2016-10-30 13:22:50 -07:00
|
|
|
tls.createSecureContext({ crl: 'not a CRL' });
|
2017-08-09 22:26:03 -05:00
|
|
|
}, (err) => {
|
|
|
|
// Throws general error, so there is no opensslErrorStack property.
|
2019-12-25 18:02:16 +01:00
|
|
|
return err instanceof Error &&
|
|
|
|
/^Error: Failed to parse CRL$/.test(err) &&
|
|
|
|
!('opensslErrorStack' in err);
|
2017-08-09 22:26:03 -05:00
|
|
|
});
|
2016-09-17 11:32:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the stream function uses utf8 as a default encoding.
|
2018-03-25 22:27:38 +08:00
|
|
|
*/
|
2016-09-17 11:32:29 +02:00
|
|
|
|
|
|
|
function testEncoding(options, assertionHash) {
|
|
|
|
const hash = crypto.createHash('sha256', options);
|
|
|
|
let hashValue = '';
|
|
|
|
|
|
|
|
hash.on('data', (data) => {
|
crypto: do not overwrite _writableState.defaultEncoding
This only affects the writable side of LazyTransform and should not
change the behavior of any LazyTransform streams (Cipher, Decipher,
Cipheriv, Decipheriv, Hash, Hmac).
If the user does not set defaultEncoding when creating a transform
stream, WritableState uses 'utf8' by default. Only LazyTransform
overwrites this with 'buffer' for strict backward compatibility. This
was necessary when crypto.DEFAULT_ENCODING still existed. Now that
DEFAULT_ENCODING has been removed, defaultEncoding is always 'buffer'.
The writable side of LazyTransform appears to treat 'utf8' and 'buffer'
in exactly the same way. Therefore, there seems to be no need to
overwrite _writableState.defaultEncoding at this point.
Nevertheless, because Node.js has failed to hide implementation details
such as _writableState from the ecosystem, we may want to consider this
a breaking change.
Refs: https://github.com/nodejs/node/pull/47182
Refs: https://github.com/nodejs/node/pull/8611
PR-URL: https://github.com/nodejs/node/pull/49140
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2023-08-27 16:12:29 +02:00
|
|
|
// The defaultEncoding has no effect on the hash value. It only affects data
|
|
|
|
// consumed by the Hash transform stream.
|
|
|
|
assert(Buffer.isBuffer(data));
|
2016-09-17 11:32:29 +02:00
|
|
|
hashValue += data.toString('hex');
|
|
|
|
});
|
|
|
|
|
|
|
|
hash.on('end', common.mustCall(() => {
|
|
|
|
assert.strictEqual(hashValue, assertionHash);
|
|
|
|
}));
|
|
|
|
|
|
|
|
hash.write('öäü');
|
|
|
|
hash.end();
|
crypto: do not overwrite _writableState.defaultEncoding
This only affects the writable side of LazyTransform and should not
change the behavior of any LazyTransform streams (Cipher, Decipher,
Cipheriv, Decipheriv, Hash, Hmac).
If the user does not set defaultEncoding when creating a transform
stream, WritableState uses 'utf8' by default. Only LazyTransform
overwrites this with 'buffer' for strict backward compatibility. This
was necessary when crypto.DEFAULT_ENCODING still existed. Now that
DEFAULT_ENCODING has been removed, defaultEncoding is always 'buffer'.
The writable side of LazyTransform appears to treat 'utf8' and 'buffer'
in exactly the same way. Therefore, there seems to be no need to
overwrite _writableState.defaultEncoding at this point.
Nevertheless, because Node.js has failed to hide implementation details
such as _writableState from the ecosystem, we may want to consider this
a breaking change.
Refs: https://github.com/nodejs/node/pull/47182
Refs: https://github.com/nodejs/node/pull/8611
PR-URL: https://github.com/nodejs/node/pull/49140
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2023-08-27 16:12:29 +02:00
|
|
|
|
|
|
|
assert.strictEqual(hash._writableState.defaultEncoding, options?.defaultEncoding ?? 'utf8');
|
2016-09-17 11:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hash of "öäü" in utf8 format
|
|
|
|
const assertionHashUtf8 =
|
|
|
|
'4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c';
|
|
|
|
|
|
|
|
// Hash of "öäü" in latin1 format
|
|
|
|
const assertionHashLatin1 =
|
|
|
|
'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830';
|
|
|
|
|
|
|
|
testEncoding(undefined, assertionHashUtf8);
|
|
|
|
testEncoding({}, assertionHashUtf8);
|
|
|
|
|
|
|
|
testEncoding({
|
|
|
|
defaultEncoding: 'utf8'
|
|
|
|
}, assertionHashUtf8);
|
|
|
|
|
|
|
|
testEncoding({
|
|
|
|
defaultEncoding: 'latin1'
|
|
|
|
}, assertionHashLatin1);
|