From 36d9786b3d0f5a7846fabe20f1564e25c5097986 Mon Sep 17 00:00:00 2001 From: Coffee Converter Date: Tue, 24 Mar 2020 15:39:16 -0500 Subject: [PATCH] fixes bug in randBits() - the bug was causing the whole first byte to be zeroed out when (bitLength % 8 === 0) - added a check of the above condition before zeroing out the extra bits --- src/main.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.js b/src/main.js index 27a5c71..795a953 100644 --- a/src/main.js +++ b/src/main.js @@ -401,8 +401,10 @@ export function randBits(bitLength, forceLength = false) { const byteLength = Math.ceil(bitLength / 8); let rndBytes = randBytesSync(byteLength, false); - // Fill with 0's the extra bits - rndBytes[0] = rndBytes[0] & (2 ** (bitLength % 8) - 1); + if (bitLength % 8) { + // Fill with 0's the extra bits + rndBytes[0] = rndBytes[0] & (2 ** (bitLength % 8) - 1); + } if (forceLength) { let mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128; rndBytes[0] = rndBytes[0] | mask;