SecurityUtils { public static String randomAlphanumeric(int length) { StringBuilder buffer = new StringBuilder(length); SecureRandom secureRandom = new SecureRandom(); for (int i = 0; i < length; i++) { double number = secureRandom.nextDouble(); int b = ((int) (number * ALPHANUM_ARRAY.length)); buffer.append(ALPHANUM_ARRAY[b]); } return buffer.toString(); }  static String randomAlphanumeric(int length); static void logOpenSslExitCode(String command, int exitCode); static String hideOpenSslPassword(String command); static String getOpenSslCommandResult(String command, int exitCode); static void writeCaConfigFile(String path); static String getKeystorePass(); static String getSecurityDir(); static void initializeSecurityParameters(MapOperations configMap); static void initializeSecurityParameters(MapOperations configMap,
                                                boolean persistPassword); static void cleanupSecurityDir(); static final String UPPER; static final String LOWER; static final String DIGITS; static final String ALPHANUM; static final char[] ALPHANUM_ARRAY; }
@Test public void testRandomAlphanumeric() throws Exception { int passLength = 50; String password = SecurityUtils.randomAlphanumeric(passLength); Assert.assertEquals( "Returned string length does not match requested length", passLength, password.length()); password = SecurityUtils.randomAlphanumeric(0); Assert.assertTrue("Returned string should be empty", password.isEmpty()); }
@Test(expected = NegativeArraySizeException.class) public void testRandomAlphanumericException() throws Exception { SecurityUtils.randomAlphanumeric(-1); }