summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2013-06-10 23:34:11 +0200
committerBen Noordhuis <info@bnoordhuis.nl>2013-06-11 13:07:24 +0200
commit82b3524bce845bfb38d99f52ce3ab6492847ce9e (patch)
tree2c3f4d65fbf41af38b7e8f26a626fa888abb30da
parentf1b878cafa37105603632b484d4203ae33664a0d (diff)
downloadnode-82b3524bce845bfb38d99f52ce3ab6492847ce9e.tar.gz
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.
-rw-r--r--lib/crypto.js1
-rw-r--r--test/simple/test-crypto.js15
2 files changed, 16 insertions, 0 deletions
diff --git a/lib/crypto.js b/lib/crypto.js
index 4650067fe..0cc70ff15 100644
--- a/lib/crypto.js
+++ b/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;
diff --git a/test/simple/test-crypto.js b/test/simple/test-crypto.js
index 1d913fe0b..36f232d67 100644
--- a/test/simple/test-crypto.js
+++ b/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.
+})();