From 82b3524bce845bfb38d99f52ce3ab6492847ce9e Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 10 Jun 2013 23:34:11 +0200 Subject: [PATCH] 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. --- lib/crypto.js | 1 + test/simple/test-crypto.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/crypto.js b/lib/crypto.js index 4650067fe6d..0cc70ff15a8 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 1d913fe0b54..36f232d6782 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. +})();