AesCtrDrbg implements Drbg { @Override public void nextBytes(byte[] bytes) { if (bytes.length <= UPDATE_LIMIT) { nextBytesBounded(new byte[bytes.length], bytes); } else { int offset = 0; byte[] temp = new byte[UPDATE_LIMIT]; byte[] zeroes = new byte[temp.length]; while (bytes.length - offset > UPDATE_LIMIT) { nextBytesBounded(zeroes, temp); System.arraycopy(temp, 0, bytes, offset, temp.length); offset += UPDATE_LIMIT; } temp = new byte[bytes.length - offset]; zeroes = new byte[bytes.length - offset]; nextBytesBounded(zeroes, temp); System.arraycopy(temp, 0, bytes, offset, temp.length); } } AesCtrDrbg();  AesCtrDrbg(byte[] seed); @Override void nextBytes(byte[] bytes); static final int SEED_LENGTH; }
@Test public void testDefaultConstructor() { Drbg drbg = new AesCtrDrbg(); Drbg otherDrbg = new AesCtrDrbg(); byte[] bytes = new byte[16]; drbg.nextBytes(bytes); byte[] otherBytes = new byte[16]; otherDrbg.nextBytes(bytes); Assert.assertThat(bytes, IsNot.not(IsEqual.equalTo(otherBytes))); }
@Test public void testNextBytesEqualSeed() { Random rand = new Random(42); final byte[] seed = new byte[32]; rand.nextBytes(seed); AesCtrDrbg drbg1 = new AesCtrDrbg(seed); AesCtrDrbg drbg2 = new AesCtrDrbg(seed); final int arraySize = 1500; byte[] bytes1 = new byte[arraySize]; byte[] bytes2 = new byte[arraySize]; drbg1.nextBytes(bytes1); drbg2.nextBytes(bytes2); assertArrayEquals(bytes1, bytes2); assertFalse(Arrays.equals(bytes1, new byte[arraySize])); drbg1.nextBytes(bytes1); assertFalse(Arrays.equals(bytes1, bytes2)); drbg2.nextBytes(bytes2); assertArrayEquals(bytes1, bytes2); assertFalse(Arrays.equals(bytes1, new byte[arraySize])); }
@Test public void testNextBytesUnequalSeed() { Random rand = new Random(42); final byte[] seed1 = new byte[32]; final byte[] seed2 = new byte[32]; rand.nextBytes(seed1); rand.nextBytes(seed2); AesCtrDrbg drbg1 = new AesCtrDrbg(seed1); AesCtrDrbg drbg2 = new AesCtrDrbg(seed2); final int arraySize = 150; byte[] bytes1 = new byte[arraySize]; byte[] bytes2 = new byte[arraySize]; drbg1.nextBytes(bytes1); drbg2.nextBytes(bytes2); assertFalse(Arrays.equals(bytes1, bytes2)); assertFalse(Arrays.equals(bytes1, new byte[arraySize])); }
@Test public void testNextBytesManyBytes() { Random rand = new Random(42); final byte[] seed1 = new byte[32]; rand.nextBytes(seed1); rand.nextBytes(seed1); AesCtrDrbg drbg1 = new AesCtrDrbg(seed1); AesCtrDrbg drbg2 = new AesCtrDrbg(seed1); final int arraySize = (1 << 20); byte[] bytes1 = new byte[arraySize]; byte[] bytes2 = new byte[arraySize]; drbg1.nextBytes(bytes1); drbg2.nextBytes(bytes2); assertArrayEquals(bytes1, bytes2); assertFalse(Arrays.equals(bytes1, new byte[arraySize])); }