Merge pull request #5 from coffee-converter/fix-randBits
Fixes bug in randBits()
This commit is contained in:
commit
3602667de4
|
@ -14,3 +14,6 @@ node_modules/
|
|||
|
||||
# Visual Studio Code
|
||||
.vscode
|
||||
|
||||
# IntelliJ
|
||||
.idea/
|
||||
|
|
|
@ -360,11 +360,14 @@ var bigintCryptoUtils = (function (exports) {
|
|||
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
||||
|
||||
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);
|
||||
const rndBytes = randBytesSync(byteLength, false);
|
||||
const bitLengthMod8 = bitLength % 8;
|
||||
if (bitLengthMod8) {
|
||||
// Fill with 0's the extra bits
|
||||
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1);
|
||||
}
|
||||
if (forceLength) {
|
||||
let mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128;
|
||||
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128;
|
||||
rndBytes[0] = rndBytes[0] | mask;
|
||||
}
|
||||
return rndBytes;
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -357,11 +357,14 @@ function randBits(bitLength, forceLength = false) {
|
|||
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
||||
|
||||
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);
|
||||
const rndBytes = randBytesSync(byteLength, false);
|
||||
const bitLengthMod8 = bitLength % 8;
|
||||
if (bitLengthMod8) {
|
||||
// Fill with 0's the extra bits
|
||||
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1);
|
||||
}
|
||||
if (forceLength) {
|
||||
let mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128;
|
||||
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128;
|
||||
rndBytes[0] = rndBytes[0] | mask;
|
||||
}
|
||||
return rndBytes;
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -376,11 +376,14 @@ function randBits(bitLength, forceLength = false) {
|
|||
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
||||
|
||||
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);
|
||||
const rndBytes = randBytesSync(byteLength, false);
|
||||
const bitLengthMod8 = bitLength % 8;
|
||||
if (bitLengthMod8) {
|
||||
// Fill with 0's the extra bits
|
||||
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1);
|
||||
}
|
||||
if (forceLength) {
|
||||
let mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128;
|
||||
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128;
|
||||
rndBytes[0] = rndBytes[0] | mask;
|
||||
}
|
||||
return rndBytes;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "bigint-crypto-utils",
|
||||
"version": "2.5.0",
|
||||
"version": "2.5.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "bigint-crypto-utils",
|
||||
"version": "2.5.0",
|
||||
"version": "2.5.1",
|
||||
"description": "Utils for working with cryptography using native JS implementation of BigInt. It includes arbitrary precision modular arithmetics, cryptographically secure random numbers and strong probable prime generation/testing.",
|
||||
"keywords": [
|
||||
"modular arithmetics",
|
||||
|
|
11
src/main.js
11
src/main.js
|
@ -400,11 +400,14 @@ export function randBits(bitLength, forceLength = false) {
|
|||
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
||||
|
||||
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);
|
||||
const rndBytes = randBytesSync(byteLength, false);
|
||||
const bitLengthMod8 = bitLength % 8;
|
||||
if (bitLengthMod8) {
|
||||
// Fill with 0's the extra bits
|
||||
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1);
|
||||
}
|
||||
if (forceLength) {
|
||||
let mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128;
|
||||
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128;
|
||||
rndBytes[0] = rndBytes[0] | mask;
|
||||
}
|
||||
return rndBytes;
|
||||
|
|
Loading…
Reference in New Issue