@Test public void generateSecret() throws Exception { BigInteger max = new BigInteger(2048, new Random()); int maxSizeInBytes = RsaKem.bigIntSizeInBytes(max); Set<String> secrets = new TreeSet<>(); for (int i = 0; i < 100; i++) { byte[] secret = RsaKem.generateSecret(max); BigInteger secretBigInt = new BigInteger(1, secret); secrets.add(new String(secret, UTF_8)); assertThat(secret).hasLength(maxSizeInBytes); assertThat(secretBigInt.signum()).isEqualTo(1); assertThat(secretBigInt.compareTo(max)).isLessThan(0); } assertThat(secrets).hasSize(100); }
static byte[] generateSecret(BigInteger max) { int maxSizeInBytes = bigIntSizeInBytes(max); Random rand = new SecureRandom(); BigInteger r; do { r = new BigInteger(max.bitLength(), rand); } while (r.signum() <= 0 || r.compareTo(max) >= 0); return bigIntToByteArray(r, maxSizeInBytes); }
RsaKem { static byte[] generateSecret(BigInteger max) { int maxSizeInBytes = bigIntSizeInBytes(max); Random rand = new SecureRandom(); BigInteger r; do { r = new BigInteger(max.bitLength(), rand); } while (r.signum() <= 0 || r.compareTo(max) >= 0); return bigIntToByteArray(r, maxSizeInBytes); } }
RsaKem { static byte[] generateSecret(BigInteger max) { int maxSizeInBytes = bigIntSizeInBytes(max); Random rand = new SecureRandom(); BigInteger r; do { r = new BigInteger(max.bitLength(), rand); } while (r.signum() <= 0 || r.compareTo(max) >= 0); return bigIntToByteArray(r, maxSizeInBytes); } private  RsaKem(); }
RsaKem { static byte[] generateSecret(BigInteger max) { int maxSizeInBytes = bigIntSizeInBytes(max); Random rand = new SecureRandom(); BigInteger r; do { r = new BigInteger(max.bitLength(), rand); } while (r.signum() <= 0 || r.compareTo(max) >= 0); return bigIntToByteArray(r, maxSizeInBytes); } private  RsaKem();  }
RsaKem { static byte[] generateSecret(BigInteger max) { int maxSizeInBytes = bigIntSizeInBytes(max); Random rand = new SecureRandom(); BigInteger r; do { r = new BigInteger(max.bitLength(), rand); } while (r.signum() <= 0 || r.compareTo(max) >= 0); return bigIntToByteArray(r, maxSizeInBytes); } private  RsaKem();   }