NonceUtil { public static ByteString generateNonce(int length) { if (length == 0) return ByteString.NULL_VALUE; byte[] bs = new byte[length]; Random random = null; if (SECURE_RANDOM_ENABLED) { random = SECURE_RANDOM.get(); } if (random == null) { random = ThreadLocalRandom.current(); } random.nextBytes(bs); return new ByteString(bs); }  static void enableSecureRandom(); static void disableSecureRandom(); static boolean isSecureRandomEnabled(); static ByteString generateNonce(int length); static ByteString generateNonce(SecurityPolicy securityPolicy); static void validateNonce(ByteString nonce, SecurityPolicy securityPolicy); static void validateNonce(ByteString nonce); static void validateNonce(ByteString nonce, int minimumLength); static final int MINIMUM_NONCE_LENGTH; }
@Test public void testSecureChannelNonceGeneration() { for (SecurityPolicy securityPolicy : SecurityPolicy.values()) { ByteString nonce = NonceUtil.generateNonce(securityPolicy); switch (securityPolicy) { case None: assertEquals(nonce.length(), 0); break; case Basic128Rsa15: assertEquals(nonce.length(), 16); break; default: assertEquals(nonce.length(), 32); } } }