@Test public void generateRandomPassword() { Assert.assertTrue(PasswordGenerator.generateRandomPassword(0).length() == 3); Assert.assertTrue(PasswordGenerator.generateRandomPassword(1).length() == 3); Assert.assertTrue(PasswordGenerator.generateRandomPassword(5).length() == 5); final String password = PasswordGenerator.generateRandomPassword(8); Assert.assertTrue(containsDigit(password)); Assert.assertTrue(containsLowercase(password)); Assert.assertTrue(containsUppercase(password)); }
public static String generateRandomPassword(final int num) { final Random r = new SecureRandom(); final StringBuilder password = new StringBuilder(); if (num < minLength) { for (int i = 0; i < minLength; i++) { password.append(generateAlphaNumeric(r)); } } else { final List<Character> passwordChars = new ArrayList<>(); passwordChars.add(generateLowercaseChar(r)); passwordChars.add(generateUppercaseChar(r)); passwordChars.add(generateDigit(r)); for (int i = passwordChars.size(); i < num; i++) { passwordChars.add(generateAlphaNumeric(r)); } Collections.shuffle(passwordChars, new SecureRandom()); for (final char c : passwordChars) { password.append(c); } } return password.toString(); }
PasswordGenerator { public static String generateRandomPassword(final int num) { final Random r = new SecureRandom(); final StringBuilder password = new StringBuilder(); if (num < minLength) { for (int i = 0; i < minLength; i++) { password.append(generateAlphaNumeric(r)); } } else { final List<Character> passwordChars = new ArrayList<>(); passwordChars.add(generateLowercaseChar(r)); passwordChars.add(generateUppercaseChar(r)); passwordChars.add(generateDigit(r)); for (int i = passwordChars.size(); i < num; i++) { passwordChars.add(generateAlphaNumeric(r)); } Collections.shuffle(passwordChars, new SecureRandom()); for (final char c : passwordChars) { password.append(c); } } return password.toString(); } }
PasswordGenerator { public static String generateRandomPassword(final int num) { final Random r = new SecureRandom(); final StringBuilder password = new StringBuilder(); if (num < minLength) { for (int i = 0; i < minLength; i++) { password.append(generateAlphaNumeric(r)); } } else { final List<Character> passwordChars = new ArrayList<>(); passwordChars.add(generateLowercaseChar(r)); passwordChars.add(generateUppercaseChar(r)); passwordChars.add(generateDigit(r)); for (int i = passwordChars.size(); i < num; i++) { passwordChars.add(generateAlphaNumeric(r)); } Collections.shuffle(passwordChars, new SecureRandom()); for (final char c : passwordChars) { password.append(c); } } return password.toString(); }  }
PasswordGenerator { public static String generateRandomPassword(final int num) { final Random r = new SecureRandom(); final StringBuilder password = new StringBuilder(); if (num < minLength) { for (int i = 0; i < minLength; i++) { password.append(generateAlphaNumeric(r)); } } else { final List<Character> passwordChars = new ArrayList<>(); passwordChars.add(generateLowercaseChar(r)); passwordChars.add(generateUppercaseChar(r)); passwordChars.add(generateDigit(r)); for (int i = passwordChars.size(); i < num; i++) { passwordChars.add(generateAlphaNumeric(r)); } Collections.shuffle(passwordChars, new SecureRandom()); for (final char c : passwordChars) { password.append(c); } } return password.toString(); }  static String generateRandomPassword(final int num); static String generatePresharedKey(final int numChars); }
PasswordGenerator { public static String generateRandomPassword(final int num) { final Random r = new SecureRandom(); final StringBuilder password = new StringBuilder(); if (num < minLength) { for (int i = 0; i < minLength; i++) { password.append(generateAlphaNumeric(r)); } } else { final List<Character> passwordChars = new ArrayList<>(); passwordChars.add(generateLowercaseChar(r)); passwordChars.add(generateUppercaseChar(r)); passwordChars.add(generateDigit(r)); for (int i = passwordChars.size(); i < num; i++) { passwordChars.add(generateAlphaNumeric(r)); } Collections.shuffle(passwordChars, new SecureRandom()); for (final char c : passwordChars) { password.append(c); } } return password.toString(); }  static String generateRandomPassword(final int num); static String generatePresharedKey(final int numChars);  }