Browse Source

crypto: fix default encoding of LazyTransform

PullRequest #5522 and #5500 described the change
of the default encoding into UTF8 in crypto functions.

This however was only changed for the non-streaming API.
The streaming API still used binary as the default encoding.

This commit will change the default streaming API encoding to UTF8
to make both APIs behave the same.

It will also add tests to validate the behavior.

Refs: https://github.com/nodejs/node/pull/5522
Refs: https://github.com/nodejs/node/pull/5500
PR-URL: https://github.com/nodejs/node/pull/8611
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
v6
Lukas Möller 8 years ago
committed by Anna Henningsen
parent
commit
443691a5ae
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 7
      lib/internal/streams/lazy_transform.js
  2. 39
      test/parallel/test-crypto.js

7
lib/internal/streams/lazy_transform.js

@ -5,6 +5,7 @@
const stream = require('stream'); const stream = require('stream');
const util = require('util'); const util = require('util');
const crypto = require('crypto');
module.exports = LazyTransform; module.exports = LazyTransform;
@ -22,7 +23,11 @@ util.inherits(LazyTransform, stream.Transform);
get: function() { get: function() {
stream.Transform.call(this, this._options); stream.Transform.call(this, this._options);
this._writableState.decodeStrings = false; this._writableState.decodeStrings = false;
this._writableState.defaultEncoding = 'latin1';
if (!this._options || !this._options.defaultEncoding) {
this._writableState.defaultEncoding = crypto.DEFAULT_ENCODING;
}
return this[prop]; return this[prop];
}, },
set: function(val) { set: function(val) {

39
test/parallel/test-crypto.js

@ -189,3 +189,42 @@ console.log(crypto.randomBytes(16));
assert.throws(function() { assert.throws(function() {
tls.createSecureContext({ crl: 'not a CRL' }); tls.createSecureContext({ crl: 'not a CRL' });
}, /^Error: Failed to parse CRL$/); }, /^Error: Failed to parse CRL$/);
/**
* Check if the stream function uses utf8 as a default encoding.
**/
function testEncoding(options, assertionHash) {
const hash = crypto.createHash('sha256', options);
let hashValue = '';
hash.on('data', (data) => {
hashValue += data.toString('hex');
});
hash.on('end', common.mustCall(() => {
assert.strictEqual(hashValue, assertionHash);
}));
hash.write('öäü');
hash.end();
}
// 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);

Loading…
Cancel
Save