PasswordUtil { public static String humanPassword(int len) { StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { int index = RANDOM.nextInt(ALPHA_NUMERIC_READABLE_CHAR.length()); sb.append(ALPHA_NUMERIC_READABLE_CHAR.charAt(index)); } return sb.toString(); } private  PasswordUtil(); static String humanPassword(int len); static String humanPassword(); static final SecureRandom RANDOM; }
@Test public void testHumanPassword() { String pw = PasswordUtil.humanPassword(10); assertEquals(pw.length(), 10); assertNotEquals(pw, PasswordUtil.humanPassword(10)); assertEquals("", PasswordUtil.humanPassword(0)); }
@Test public void testExcludedChars() { List<Character> disallowedChars = Arrays.asList('Q', '8', 'B', 'Z', 'G', '6', '0', 'O', 'o', '1', 'i', 'I', '5', 'S', 's'); for (int i = 0; i < 10000; i++) { String pw = PasswordUtil.humanPassword(20); for (int e = 0; e < pw.length(); e++) { char pwChar = pw.charAt(e); assertFalse("Found disallowed character in pw {" + pw + "} - {" + pwChar + "}", disallowedChars.contains(pwChar)); } } }