From 68c2a6ea1d909b0e37a641fe2f9831d5168fe34f Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Fri, 19 Jan 2018 00:44:15 +0100 Subject: [PATCH] Hex strings no longer need have even number of digits If a hex string has an odd number of bytes, the last byte is just ignored in Node.js. --- index.js | 2 -- test/node/test-buffer-alloc.js | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 6944c05..a6d094a 100644 --- a/index.js +++ b/index.js @@ -752,9 +752,7 @@ function hexWrite (buf, string, offset, length) { } } - // must be an even number of digits var strLen = string.length - if (strLen % 2 !== 0) throw new TypeError('Invalid hex string') if (length > strLen / 2) { length = strLen / 2 diff --git a/test/node/test-buffer-alloc.js b/test/node/test-buffer-alloc.js index c813a23..dd4799c 100644 --- a/test/node/test-buffer-alloc.js +++ b/test/node/test-buffer-alloc.js @@ -516,13 +516,13 @@ assert.deepStrictEqual(Buffer.from(' YWJvcnVtLg', 'base64'), assert.strictEqual(hexb2[i], hexb[i]); } } -/* + // Test single hex character is discarded. assert.strictEqual(Buffer.from('A', 'hex').length, 0); // Test that if a trailing character is discarded, rest of string is processed. assert.deepStrictEqual(Buffer.from('Abx', 'hex'), Buffer.from('Ab', 'hex')); -*/ + // Test single base64 char encodes as 0. assert.strictEqual(Buffer.from('A', 'base64').length, 0); -- 2.34.1