what-the-bank/sources/org/bouncycastle/crypto/generators/CramerShoupParametersGenera...

73 lines
2.9 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.generators;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.params.CramerShoupParameters;
import org.bouncycastle.crypto.params.DHParameters;
import org.bouncycastle.util.BigIntegers;
/* loaded from: classes6.dex */
public class CramerShoupParametersGenerator {
private static final BigInteger ONE = BigInteger.valueOf(1);
private int certainty;
private SecureRandom random;
private int size;
public void init(int i, int i2, SecureRandom secureRandom) {
this.size = i;
this.certainty = i2;
this.random = secureRandom;
}
/* loaded from: classes6.dex */
static class ParametersHelper {
private static final BigInteger TWO = BigInteger.valueOf(2);
static BigInteger selectGenerator(BigInteger bigInteger, SecureRandom secureRandom) {
BigInteger modPow;
BigInteger subtract = bigInteger.subtract(TWO);
do {
BigInteger bigInteger2 = TWO;
modPow = BigIntegers.createRandomInRange(bigInteger2, subtract, secureRandom).modPow(bigInteger2, bigInteger);
} while (modPow.equals(CramerShoupParametersGenerator.ONE));
return modPow;
}
static BigInteger[] generateSafePrimes(int i, int i2, SecureRandom secureRandom) {
BigInteger bigInteger;
BigInteger add;
while (true) {
bigInteger = new BigInteger(i - 1, 2, secureRandom);
add = bigInteger.shiftLeft(1).add(CramerShoupParametersGenerator.ONE);
if (!add.isProbablePrime(i2) || (i2 > 2 && !bigInteger.isProbablePrime(i2))) {
}
}
return new BigInteger[]{add, bigInteger};
}
private ParametersHelper() {
}
}
public CramerShoupParameters generateParameters(DHParameters dHParameters) {
BigInteger selectGenerator;
BigInteger p = dHParameters.getP();
BigInteger g = dHParameters.getG();
do {
selectGenerator = ParametersHelper.selectGenerator(p, this.random);
} while (g.equals(selectGenerator));
return new CramerShoupParameters(p, g, selectGenerator, new SHA256Digest());
}
public CramerShoupParameters generateParameters() {
BigInteger selectGenerator;
BigInteger bigInteger = ParametersHelper.generateSafePrimes(this.size, this.certainty, this.random)[1];
BigInteger selectGenerator2 = ParametersHelper.selectGenerator(bigInteger, this.random);
do {
selectGenerator = ParametersHelper.selectGenerator(bigInteger, this.random);
} while (selectGenerator2.equals(selectGenerator));
return new CramerShoupParameters(bigInteger, selectGenerator2, selectGenerator, new SHA256Digest());
}
}