From 74b56cd775a517d3e404893643ce958940e3395b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Mo=CC=88ller?= Date: Sat, 17 Sep 2016 11:32:29 +0200 Subject: [PATCH 1/2] crypto: fix default encoding of LazyTransform Change the default encoding from latin1 to utf8 and extend the test-crypto tests. --- lib/internal/streams/lazy_transform.js | 7 ++++- test/parallel/test-crypto.js | 39 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/internal/streams/lazy_transform.js b/lib/internal/streams/lazy_transform.js index bd68bef4b6dd17..3607d985346992 100644 --- a/lib/internal/streams/lazy_transform.js +++ b/lib/internal/streams/lazy_transform.js @@ -5,6 +5,7 @@ const stream = require('stream'); const util = require('util'); +const crypto = require('crypto'); module.exports = LazyTransform; @@ -22,7 +23,11 @@ util.inherits(LazyTransform, stream.Transform); get: function() { stream.Transform.call(this, this._options); this._writableState.decodeStrings = false; - this._writableState.defaultEncoding = 'latin1'; + + if (!this._options || !this._options.defaultEncoding) { + this._writableState.defaultEncoding = crypto.DEFAULT_ENCODING; + } + return this[prop]; }, set: function(val) { diff --git a/test/parallel/test-crypto.js b/test/parallel/test-crypto.js index 2e94397c8f22f9..b0b273587a1495 100644 --- a/test/parallel/test-crypto.js +++ b/test/parallel/test-crypto.js @@ -168,3 +168,42 @@ console.log(crypto.randomBytes(16)); assert.throws(function() { tls.createSecureContext({ crl: 'not a 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', () => { + assert.equal(hashValue, assertionHash); + }); + + hash.write('öäü'); + hash.end(); +} + +// Hash of "öäü" in utf8 format +const assertionHashUtf8 = + '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c'; + +// Hash of "öäü" in ascii format +const assertionHashAscii = + 'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830'; + +testEncoding(undefined, assertionHashUtf8); +testEncoding({}, assertionHashUtf8); + +testEncoding({ + defaultEncoding: 'utf8' +}, assertionHashUtf8); + +testEncoding({ + defaultEncoding: 'ascii' +}, assertionHashAscii); From fe9de5d880ed0d6b22e70dd2d7f24e017f0bfdbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Mo=CC=88ller?= Date: Mon, 19 Sep 2016 09:42:17 +0200 Subject: [PATCH 2/2] 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. --- test/parallel/test-crypto.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/parallel/test-crypto.js b/test/parallel/test-crypto.js index b0b273587a1495..c77ac2d2e59a46 100644 --- a/test/parallel/test-crypto.js +++ b/test/parallel/test-crypto.js @@ -181,9 +181,9 @@ function testEncoding(options, assertionHash) { hashValue += data.toString('hex'); }); - hash.on('end', () => { - assert.equal(hashValue, assertionHash); - }); + hash.on('end', common.mustCall(() => { + assert.strictEqual(hashValue, assertionHash); + })); hash.write('öäü'); hash.end(); @@ -193,8 +193,8 @@ function testEncoding(options, assertionHash) { const assertionHashUtf8 = '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c'; -// Hash of "öäü" in ascii format -const assertionHashAscii = +// Hash of "öäü" in latin1 format +const assertionHashLatin1 = 'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830'; testEncoding(undefined, assertionHashUtf8); @@ -205,5 +205,5 @@ testEncoding({ }, assertionHashUtf8); testEncoding({ - defaultEncoding: 'ascii' -}, assertionHashAscii); + defaultEncoding: 'latin1' +}, assertionHashLatin1);