70 lines
2.5 KiB
Java
70 lines
2.5 KiB
Java
package org.bouncycastle.util;
|
|
|
|
import java.math.BigInteger;
|
|
import java.security.SecureRandom;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public final class BigIntegers {
|
|
private static final int MAX_ITERATIONS = 1000;
|
|
private static final BigInteger ZERO = BigInteger.valueOf(0);
|
|
|
|
public static BigInteger fromUnsignedByteArray(byte[] bArr, int i, int i2) {
|
|
if (i != 0 || i2 != bArr.length) {
|
|
byte[] bArr2 = new byte[i2];
|
|
System.arraycopy(bArr, i, bArr2, 0, i2);
|
|
bArr = bArr2;
|
|
}
|
|
return new BigInteger(1, bArr);
|
|
}
|
|
|
|
public static BigInteger fromUnsignedByteArray(byte[] bArr) {
|
|
return new BigInteger(1, bArr);
|
|
}
|
|
|
|
public static BigInteger createRandomInRange(BigInteger bigInteger, BigInteger bigInteger2, SecureRandom secureRandom) {
|
|
int compareTo = bigInteger.compareTo(bigInteger2);
|
|
if (compareTo >= 0) {
|
|
if (compareTo <= 0) {
|
|
return bigInteger;
|
|
}
|
|
throw new IllegalArgumentException("'min' may not be greater than 'max'");
|
|
}
|
|
if (bigInteger.bitLength() > bigInteger2.bitLength() / 2) {
|
|
return createRandomInRange(ZERO, bigInteger2.subtract(bigInteger), secureRandom).add(bigInteger);
|
|
}
|
|
for (int i = 0; i < 1000; i++) {
|
|
BigInteger bigInteger3 = new BigInteger(bigInteger2.bitLength(), secureRandom);
|
|
if (bigInteger3.compareTo(bigInteger) >= 0 && bigInteger3.compareTo(bigInteger2) <= 0) {
|
|
return bigInteger3;
|
|
}
|
|
}
|
|
return new BigInteger(bigInteger2.subtract(bigInteger).bitLength() - 1, secureRandom).add(bigInteger);
|
|
}
|
|
|
|
public static byte[] asUnsignedByteArray(BigInteger bigInteger) {
|
|
byte[] byteArray = bigInteger.toByteArray();
|
|
if (byteArray[0] != 0) {
|
|
return byteArray;
|
|
}
|
|
int length = byteArray.length - 1;
|
|
byte[] bArr = new byte[length];
|
|
System.arraycopy(byteArray, 1, bArr, 0, length);
|
|
return bArr;
|
|
}
|
|
|
|
public static byte[] asUnsignedByteArray(int i, BigInteger bigInteger) {
|
|
byte[] byteArray = bigInteger.toByteArray();
|
|
if (byteArray.length == i) {
|
|
return byteArray;
|
|
}
|
|
int i2 = byteArray[0] == 0 ? 1 : 0;
|
|
int length = byteArray.length - i2;
|
|
if (length > i) {
|
|
throw new IllegalArgumentException("standard length exceeded for value");
|
|
}
|
|
byte[] bArr = new byte[i];
|
|
System.arraycopy(byteArray, i2, bArr, i - length, length);
|
|
return bArr;
|
|
}
|
|
}
|