RSAPrivateKey.generate constructor
RSAPrivateKey.generate(
- int keySize, {
- BigInt? publicExponent,
})
Implementation
factory RSAPrivateKey.generate(int keySize, {BigInt? publicExponent}) {
publicExponent ??= BigInt.from(0x01001);
BigInt p;
while (true) {
p = randomPrimeBigInt(keySize ~/ 2);
if (p % publicExponent == BigInt.one) {
continue;
}
if (publicExponent.gcd(p - BigInt.one) == BigInt.one) {
break;
}
}
BigInt q;
BigInt n;
int qBitLength = keySize - p.bitLength;
while (true) {
q = randomPrimeBigInt(qBitLength);
if (p == q) {
continue;
}
if (q % publicExponent == BigInt.one) {
continue;
}
if (publicExponent.gcd(q - BigInt.one) != BigInt.one) {
continue;
}
n = p * q;
final nBitlength = n.bitLength;
if (nBitlength != keySize) {
continue;
}
if (p < q) {
BigInt tmp = p;
p = q;
q = tmp;
}
return RSAPrivateKey.fromPrimaries(p, q, publicExponent: publicExponent);
}
}