Skip to content

crypto: fix default encoding of LazyTransform [2] #8624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const timingSafeEqual = binding.timingSafeEqual;
const Buffer = require('buffer').Buffer;
const stream = require('stream');
const util = require('util');
const LazyTransform = require('internal/streams/lazy_transform');
const LazyTransform = require('internal/crypto/lazy_transform');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file should probably still be in /streams?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lazy_transform internal class is only used by the crypto module. I discussed this with @zvictor at the code and learn this last weekend and we went through and made sure. I'm happy with moving it in order to make the relationship between this class and the crypto module clear.


const DH_GENERATOR = 2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const stream = require('stream');
const util = require('util');
const crypto = require('crypto');

module.exports = LazyTransform;

Expand All @@ -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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to use defaultEncoding of options in the following way?

if (this._options && this._options.defaultEncoding) {
  this._writableState.defaultEncoding = this._options.defaultEncoding;
} else {
  this._writableState.defaultEncoding = crypto.DEFAULT_ENCODING;
}


return this[prop];
},
set: function(val) {
Expand Down
2 changes: 1 addition & 1 deletion node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
'lib/internal/util.js',
'lib/internal/v8_prof_polyfill.js',
'lib/internal/v8_prof_processor.js',
'lib/internal/streams/lazy_transform.js',
'lib/internal/crypto/lazy_transform.js',
'lib/internal/streams/BufferList.js',
'deps/v8/tools/splaytree.js',
'deps/v8/tools/codemap.js',
Expand Down
69 changes: 69 additions & 0 deletions test/parallel/test-crypto-lazy-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';
const common = require('../common');
const assert = require('assert');

if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}

const crypto = require('crypto');

const UTF_INPUT = 'öäü';
const TEST_CASES = [
{
options: undefined,
// Hash of "öäü" in default format (utf8)
output: '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c',
},
{
options: {},
// Hash of "öäü" in default format (utf8)
output: '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c',
},
{
options: {
defaultEncoding: 'latin1',
},
// Hash of "öäü" in latin1 format
output: 'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830',
},
{
options: {
defaultEncoding: 'utf8',
},
// Hash of "öäü" in utf8 format
output: '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c',
},
{
options: {
defaultEncoding: 'ascii',
},
// Hash of "öäü" in ascii format
output: 'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830',
},
];

for (const test of TEST_CASES) {
const hashValue = crypto.createHash('sha256')
.update(UTF_INPUT, test.options && test.options.defaultEncoding)
.digest('hex');

assert.equal(hashValue, test.output);
}

for (const test of TEST_CASES) {
const hash = crypto.createHash('sha256', test.options);
let hashValue = '';

hash.on('data', (data) => {
hashValue += data.toString('hex');
});

hash.on('end', common.mustCall(() => {
assert.equal(hashValue, test.output);
}));

hash.write(UTF_INPUT);
hash.end();
}
1 change: 0 additions & 1 deletion test/parallel/test-crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ assert.throws(function() {
crypto.createHash('sha1').update({foo: 'bar'});
}, /buffer/);


function assertSorted(list) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems not necessary :)

// Array#sort() modifies the list in place so make a copy.
var sorted = util._extend([], list).sort();
Expand Down