Browse Source

crypto: fix utf8/utf-8 encoding check

Normalize the encoding in getEncoding() before using it. Fixes a
"AssertionError: Cannot change encoding" exception when the caller
mixes "utf8" and "utf-8".

Fixes #5655.
v0.10.11-release
Ben Noordhuis 12 years ago
parent
commit
82b3524bce
  1. 1
      lib/crypto.js
  2. 15
      test/simple/test-crypto.js

1
lib/crypto.js

@ -236,6 +236,7 @@ Hmac.prototype._transform = Hash.prototype._transform;
function getDecoder(decoder, encoding) {
if (encoding === 'utf-8') encoding = 'utf8'; // Normalize encoding.
decoder = decoder || new StringDecoder(encoding);
assert(decoder.encoding === encoding, 'Cannot change encoding');
return decoder;

15
test/simple/test-crypto.js

@ -900,3 +900,18 @@ assert.throws(function() {
c.update('update');
c.final();
})();
// #5655 regression tests, 'utf-8' and 'utf8' are identical.
(function() {
var c = crypto.createCipher('aes192', '0123456789abcdef');
c.update('update', ''); // Defaults to "utf8".
c.final('utf-8'); // Should not throw.
c = crypto.createCipher('aes192', '0123456789abcdef');
c.update('update', 'utf8');
c.final('utf-8'); // Should not throw.
c = crypto.createCipher('aes192', '0123456789abcdef');
c.update('update', 'utf-8');
c.final('utf8'); // Should not throw.
})();

Loading…
Cancel
Save