-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to use 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) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,6 @@ assert.throws(function() { | |
crypto.createHash('sha1').update({foo: 'bar'}); | ||
}, /buffer/); | ||
|
||
|
||
function assertSorted(list) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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.