Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function genKey(bits) {
assert((bits >>> 0) === bits);
assert(bits === 2048 || bits === 4096);
const primes = [primes1024, primes2048][bits >>> 12];
for (;;) {
const i = rng.randomRange(0, primes.length);
const j = rng.randomRange(0, primes.length);
if (i === j)
continue;
const p = BN.decode(primes[i]);
const q = BN.decode(primes[j]);
const n = p.mul(q);
const pm1 = p.subn(1);
const qm1 = q.subn(1);
const phi = pm1.mul(qm1);
let e = null;
let d = null;
for (let i = 1; i < smallPrimes.length; i++) {
try {
e = new BN(smallPrimes[i]);
d = e.invert(phi);
} catch (e) {
continue;
}
function genKey(bits) {
assert((bits >>> 0) === bits);
assert(bits === 2048 || bits === 4096);
const primes = [primes1024, primes2048][bits >>> 12];
for (;;) {
const i = rng.randomRange(0, primes.length);
const j = rng.randomRange(0, primes.length);
if (i === j)
continue;
const p = BN.decode(primes[i]);
const q = BN.decode(primes[j]);
const n = p.mul(q);
const pm1 = p.subn(1);
const qm1 = q.subn(1);
const phi = pm1.mul(qm1);
let e = null;
let d = null;
for (let i = 1; i < smallPrimes.length; i++) {
try {
e = new BN(smallPrimes[i]);
d = e.invert(phi);
} catch (e) {
continue;
}
break;
randomBits(bits) {
assert((bits >>> 0) === bits);
const ret = this.save;
let total = this.total;
while (total < bits) {
const x = BN.decode(this.generate(32));
ret.iushln(256);
ret.iadd(x);
total += 256;
}
const left = total - bits;
this.save = ret.maskn(left);
this.total = left;
ret.iushrn(left);
return ret;
}
nextRandom() {
return BN.decode(this.source.generate(32));
}
constructor(n, g, h, bits) {
if (bits == null)
bits = 0;
assert(Buffer.isBuffer(n));
assert((g >>> 0) === g);
assert((h >>> 0) === h);
assert((bits >>> 0) === bits);
this.n = BN.decode(n);
this.red = BN.red(this.n);
this.g = new BN(g).toRed(this.red);
this.h = new BN(h).toRed(this.red);
this.nh = this.n.ushrn(1);
this.bits = this.n.bitLength();
this.size = (this.bits + 7) >>> 3;
this.randBits = this.bits - 1;
this.groupHash = SHA256.multi(this.g.fromRed().encode('be', 4),
this.h.fromRed().encode('be', 4),
this.n.encode('be'));
this.zero = new BN(0).toRed(this.red);
this.one = new BN(1).toRed(this.red);
verify(msg, sig, C1) {
assert(Buffer.isBuffer(msg));
assert(Buffer.isBuffer(sig));
assert(Buffer.isBuffer(C1));
if (C1.length !== this.size)
return false;
let S;
try {
S = Signature.decode(sig, this.bits);
} catch (e) {
return false;
}
const C = BN.decode(C1);
try {
return this._verify(msg, S, C);
} catch (e) {
return false;
}
}
validate(s_prime, C1, key) {
assert(Buffer.isBuffer(s_prime));
assert(Buffer.isBuffer(C1));
if (s_prime.length !== 32)
return false;
if (C1.length !== this.size)
return false;
if (!isSanePrivateKey(key))
return false;
const C = BN.decode(C1);
const p = BN.decode(key.p);
const q = BN.decode(key.q);
try {
return this._validate(s_prime, C, p, q);
} catch (e) {
return false;
}
}
validate(s_prime, C1, key) {
assert(Buffer.isBuffer(s_prime));
assert(Buffer.isBuffer(C1));
if (s_prime.length !== 32)
return false;
if (C1.length !== this.size)
return false;
if (!isSanePrivateKey(key))
return false;
const C = BN.decode(C1);
const p = BN.decode(key.p);
const q = BN.decode(key.q);
try {
return this._validate(s_prime, C, p, q);
} catch (e) {
return false;
}
}