| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,7 @@ const { | |||
| 27 | 27 | ERR_CRYPTO_INVALID_STATE, | |
| 28 | 28 | ERR_INVALID_ARG_TYPE, | |
| 29 | 29 | ERR_INVALID_ARG_VALUE, | |
| 30 | + ERR_UNKNOWN_ENCODING, | ||
| 30 | 31 | } | |
| 31 | 32 | } = require('internal/errors'); | |
| 32 | 33 | ||
@@ -91,9 +92,14 @@ const privateDecrypt = rsaFunctionFor(_privateDecrypt, RSA_PKCS1_OAEP_PADDING, | |||
| 91 | 92 | 'private'); | |
| 92 | 93 | ||
| 93 | 94 | function getDecoder(decoder, encoding) { | |
| 94 | - encoding = normalizeEncoding(encoding); | ||
| 95 | + const normalizedEncoding = normalizeEncoding(encoding); | ||
| 95 | 96 | decoder = decoder || new StringDecoder(encoding); | |
| 96 | - assert(decoder.encoding === encoding, 'Cannot change encoding'); | ||
| 97 | + if (decoder.encoding !== normalizedEncoding) { | ||
| 98 | + if (normalizedEncoding === undefined) { | ||
| 99 | + throw new ERR_UNKNOWN_ENCODING(encoding); | ||
| 100 | + } | ||
| 101 | + assert(false, 'Cannot change encoding'); | ||
| 102 | + } | ||
| 97 | 103 | return decoder; | |
| 98 | 104 | } | |
| 99 | 105 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,52 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + if (!common.hasCrypto) | ||
| 4 | + common.skip('missing crypto'); | ||
| 5 | + | ||
| 6 | + // This test checks if error is thrown in case of wrong encoding provided into cipher. | ||
| 7 | + | ||
| 8 | + const assert = require('assert'); | ||
| 9 | + const { createCipheriv, randomBytes } = require('crypto'); | ||
| 10 | + | ||
| 11 | + const createCipher = () => { | ||
| 12 | + return createCipheriv('aes-256-cbc', randomBytes(32), randomBytes(16)); | ||
| 13 | + }; | ||
| 14 | + | ||
| 15 | + { | ||
| 16 | + const cipher = createCipher(); | ||
| 17 | + cipher.update('test', 'utf-8', 'utf-8'); | ||
| 18 | + | ||
| 19 | + assert.throws( | ||
| 20 | + () => cipher.update('666f6f', 'hex', 'hex'), | ||
| 21 | + { message: /Cannot change encoding/ } | ||
| 22 | + ); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + { | ||
| 26 | + const cipher = createCipher(); | ||
| 27 | + cipher.update('test', 'utf-8', 'utf-8'); | ||
| 28 | + | ||
| 29 | + assert.throws( | ||
| 30 | + () => cipher.final('hex'), | ||
| 31 | + { message: /Cannot change encoding/ } | ||
| 32 | + ); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + { | ||
| 36 | + const cipher = createCipher(); | ||
| 37 | + cipher.update('test', 'utf-8', 'utf-8'); | ||
| 38 | + | ||
| 39 | + assert.throws( | ||
| 40 | + () => cipher.final('bad2'), | ||
| 41 | + { message: /^Unknown encoding: bad2$/, code: 'ERR_UNKNOWN_ENCODING' } | ||
| 42 | + ); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + { | ||
| 46 | + const cipher = createCipher(); | ||
| 47 | + | ||
| 48 | + assert.throws( | ||
| 49 | + () => cipher.update('test', 'utf-8', 'bad3'), | ||
| 50 | + { message: /^Unknown encoding: bad3$/, code: 'ERR_UNKNOWN_ENCODING' } | ||
| 51 | + ); | ||
| 52 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments