public byte[] generateSeed(int numBytes) { byte[] bytes = new byte[numBytes]; this.nextBytes(bytes); return bytes; } }
public byte[] generateSeed(int numBytes) { byte[] data = new byte[numBytes]; // after 20 samples we'll start to check if there is new seed material. if (samples.getAndIncrement() > 20) { if (seedAvailable.getAndSet(false)) { samples.set(0); drbg.reseed((byte[])null); // need for Java 1.9 } } drbg.nextBytes(data); return data; }
/** * Generates a random IV according to NIST <a href="http://goo.gl/S9z8qF">SP-800-63a</a>, appendix C, method 2 * (pseudorandom), suitable for use with any block cipher mode described in that standard. * * @param prng NIST SP800-63a approved pseudorandom number generator. * @param blockSize Cipher block size in bytes. * * @return Cipher block size number of random bytes. */ public static byte[] nist80063a(final SP800SecureRandom prng, final int blockSize) { prng.setSeed(randomNonce(blockSize)); final byte[] iv = new byte[blockSize]; prng.nextBytes(iv); return iv; }
@Test public void reseedsUsingQuickEntropyOnEachNextByteCall() { final QuickEntropy quickEntropy = mock(QuickEntropy.class); final SP800SecureRandomBuilder sp800Builder = mock(SP800SecureRandomBuilder.class); final SP800SecureRandom sp800SecureRandom = mock(SP800SecureRandom.class); final byte[] entropy = {1, 2, 3, 4}; when(quickEntropy.getQuickEntropy()).thenReturn(entropy); when(sp800Builder.setSecurityStrength(anyInt())).thenReturn(sp800Builder); when(sp800Builder.setPersonalizationString(any())).thenReturn(sp800Builder); when(sp800Builder.buildHash(any(), any(), anyBoolean())).thenReturn(sp800SecureRandom); final PRNGSecureRandom prngSecureRandom = new PRNGSecureRandom(quickEntropy, sp800Builder); final byte[] bytes = new byte[] {}; prngSecureRandom.nextBytes(bytes); verify(quickEntropy, times(1)).getQuickEntropy(); verify(sp800SecureRandom).setSeed(entropy); verify(sp800SecureRandom).nextBytes(bytes); } }