private static long longRandomPrime() { BigInteger prime = BigInteger.probablePrime(31, new Random()); return prime.longValue(); }
private static Callable<BigInteger> generateProbablePrimes() { return () -> { Random random = new Random(); // To keep the CPU busy, we must prevent the Java and JIT compilers from optimizing this loop // out of existence. To do that, we save the the most recently generated prime, and return it // when the task is cancelled. Our assumption is that the compilers can't predict that the // return value is unused, and is therefore forced to calculate a new prime on each iteration. BigInteger prime = BigInteger.valueOf(0); while (!Thread.currentThread().isInterrupted()) { prime = BigInteger.probablePrime(1000, random); } return prime; }; }
@GwtIncompatible // isPrime is GWT-incompatible public void testIsPrimeOnRandomComposites() { Random rand = new Random(1); for (int bits = 5; bits < 32; bits++) { for (int i = 0; i < 100; i++) { long p = BigInteger.probablePrime(bits, rand).longValue(); long q = BigInteger.probablePrime(bits, rand).longValue(); assertFalse(LongMath.isPrime(p * q)); } } }
@GwtIncompatible // isPrime is GWT-incompatible public void testIsPrimeOnRandomPrimes() { Random rand = new Random(1); for (int bits = 10; bits < 63; bits++) { for (int i = 0; i < 100; i++) { long p = BigInteger.probablePrime(bits, rand).longValue(); assertTrue(LongMath.isPrime(p)); } } }
private List<BlockInfo> generateBlockInfos(int count) { List<BlockInfo> blockInfos = new ArrayList<>(); for (int i = 0; i < count; i++) { BlockInfo blockInfo = new BlockInfo(); blockInfo.setHash(sha3(ByteUtil.intToBytes(i))); blockInfo.setTotalDifficulty(BigInteger.probablePrime(512, rnd)); blockInfo.setMainChain(rnd.nextBoolean()); blockInfos.add(blockInfo); } return blockInfos; }
public void run() { while(true) BigInteger.probablePrime(MAX_PRIORITY, new Random()); }; }.start();
int bitLength = 1024; SecureRandom rnd = new SecureRandom(); BigInteger p = BigInteger.probablePrime(bitLength, rnd); BigInteger g = BigInteger.probablePrime(bitLength, rnd);
public static BigInteger createPrimeBigger(BigInteger valueThatDeterminesNumberOfBits, Random random) { int numbits = valueThatDeterminesNumberOfBits.bitLength() + 1; BigInteger ret = BigInteger.probablePrime(numbits, random); return ret; }
BigInteger definitePrime(int bits, Random rnd) { BigInteger prime = new BigInteger("4"); while(!isPrime(prime)) prime = BigInteger.probablePrime(bits,rnd); return prime; }
/** generate a random 31 bit prime **/ private static long longRandomPrime() { BigInteger prime = BigInteger.probablePrime(31, new Random()); return prime.longValue(); }
private static long longRandomPrime() { BigInteger prime = BigInteger.probablePrime(31, new Random()); return prime.longValue(); }
private static long longRandomPrime() { BigInteger prime = BigInteger.probablePrime(31, new Random()); return prime.longValue(); }
public RSA(int n) { this.n = n; BigInteger p = BigInteger.probablePrime(n / 2, random); BigInteger q = BigInteger.probablePrime(n / 2, random); BigInteger phi = (p.subtract(one)).multiply(q.subtract(one)); modulus = p.multiply(q); publicKey = new BigInteger("65537"); // common value in practice = 2^16 + 1 privateKey = publicKey.modInverse(phi); }
public RSA(int n) { this.n = n; BigInteger p = BigInteger.probablePrime(n / 2, random); BigInteger q = BigInteger.probablePrime(n / 2, random); BigInteger phi = (p.subtract(one)).multiply(q.subtract(one)); modulus = p.multiply(q); publicKey = new BigInteger("65537"); // common value in practice = 2^16 + 1 privateKey = publicKey.modInverse(phi); }
private void crashingGenerateKeyPair(SecureRandom random, int primesBits) { BigInteger p = BigInteger.probablePrime(primesBits, random); BigInteger q = BigInteger.probablePrime(primesBits, random); n = p.multiply(q); BigInteger et = p.add(BigInteger.ONE.negate()).multiply(q.add(BigInteger.ONE.negate())); e = BigInteger.valueOf(3); d = e.modInverse(et); }
BigInteger e = new BigInteger("65537"); //to make the compiler happy BigInteger counter = (BigInteger.probablePrime(128, random)).mod(phi); while(counter.intValue() > 2) { if((e.gcd(phi)).intValue() == 1) { e = counter; break; } counter = counter.subtract(BigInteger.ONE); }
@GwtIncompatible // isPrime is GWT-incompatible public void testIsPrimeOnRandomComposites() { Random rand = new Random(1); for (int bits = 5; bits < 32; bits++) { for (int i = 0; i < 100; i++) { long p = BigInteger.probablePrime(bits, rand).longValue(); long q = BigInteger.probablePrime(bits, rand).longValue(); assertFalse(LongMath.isPrime(p * q)); } } }
@GwtIncompatible // isPrime is GWT-incompatible public void testIsPrimeOnRandomPrimes() { Random rand = new Random(1); for (int bits = 10; bits < 63; bits++) { for (int i = 0; i < 100; i++) { long p = BigInteger.probablePrime(bits, rand).longValue(); assertTrue(LongMath.isPrime(p)); } } }
@Test(groups = TCKConstants.TEST_GROUP_DEBUG, description = "Validate how to use the HS256 signature alg") public void testHS256() throws Exception { JWTClaimsSet claimsSet = JWTClaimsSet.parse("{\"sub\":\"jdoe\"}"); SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet); SecureRandom random = new SecureRandom(); BigInteger secret = BigInteger.probablePrime(256, random); JWSSigner signer = new MACSigner(secret.toByteArray()); signedJWT.sign(signer); }
private X509Certificate generateRootCertificate(String commonName, Date notBefore, Date notAfter) throws Exception { X500Name issuer = new X500Name(commonName); BigInteger serial = BigInteger.probablePrime(16, new Random()); SubjectPublicKeyInfo pubKeyInfo = convertToSubjectPublicKeyInfo(m_caKey.getPublic()); X509v3CertificateBuilder builder = new X509v3CertificateBuilder(issuer, serial, notBefore, notAfter, issuer, pubKeyInfo); builder.addExtension(new Extension(Extension.basicConstraints, true, new DEROctetString(new BasicConstraints(true)))); X509CertificateHolder certHolder = builder.build(new JcaContentSignerBuilder(SIGNATURE_ALGORITHM).build(m_caKey.getPrivate())); return new JcaX509CertificateConverter().getCertificate(certHolder); } }