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
This commit is contained in:
parent
7bc770bb8c
commit
36d9786b3d
|
@ -401,8 +401,10 @@ export function randBits(bitLength, forceLength = false) {
|
|||
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
let rndBytes = randBytesSync(byteLength, false);
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue