File tree Expand file tree Collapse file tree
main/java/com/cloud/utils
test/java/com/cloud/utils Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020package com .cloud .utils ;
2121
2222import java .security .SecureRandom ;
23+ import java .util .ArrayList ;
24+ import java .util .Collections ;
25+ import java .util .List ;
2326import java .util .Random ;
2427
2528/**
@@ -48,14 +51,19 @@ public static String generateRandomPassword(int num) {
4851 password .append (generateAlphaNumeric (r ));
4952 }
5053 } else {
51- // Generate random 3-character string with a lowercase character,
52- // uppercase character, and a digit
53- password .append (generateLowercaseChar (r )).append (generateUppercaseChar (r )).append (generateDigit (r ));
54-
55- // Generate a random n-character string with only lowercase
56- // characters
57- for (int i = 0 ; i < num - 3 ; i ++) {
58- password .append (generateLowercaseChar (r ));
54+ List <Character > passwordChars = new ArrayList <Character >();
55+ passwordChars .add (generateLowercaseChar (r ));
56+ passwordChars .add (generateUppercaseChar (r ));
57+ passwordChars .add (generateDigit (r ));
58+
59+ for (int i = passwordChars .size (); i < num ; i ++) {
60+ passwordChars .add (generateAlphaNumeric (r ));
61+ }
62+
63+ Collections .shuffle (passwordChars , new SecureRandom ());
64+
65+ for (char c : passwordChars ) {
66+ password .append (c );
5967 }
6068 }
6169
@@ -87,4 +95,5 @@ public static String generatePresharedKey(int numChars) {
8795 return psk .toString ();
8896
8997 }
98+
9099}
Original file line number Diff line number Diff line change @@ -30,13 +30,36 @@ public void generateRandomPassword() {
3030 Assert .assertTrue (PasswordGenerator .generateRandomPassword (1 ).length () == 3 );
3131 Assert .assertTrue (PasswordGenerator .generateRandomPassword (5 ).length () == 5 );
3232 String password = PasswordGenerator .generateRandomPassword (8 );
33- // TODO: this might give more help to bruteforcing than desired
34- // the actual behavior is that the first character is a random lowercase
35- // char
36- Assert .assertTrue (Character .isLowerCase (password .charAt (0 )));
37- // the second character is a random upper case char
38- Assert .assertTrue (Character .isUpperCase (password .charAt (1 )));
39- // and the third is a digit
40- Assert .assertTrue (Character .isDigit (password .charAt (2 )));
33+
34+ Assert .assertTrue (containsDigit (password ));
35+ Assert .assertTrue (containsLowercase (password ));
36+ Assert .assertTrue (containsUppercase (password ));
37+ }
38+
39+ private boolean containsUppercase (String password ) {
40+ for (char c : password .toCharArray ()) {
41+ if (Character .isUpperCase (c )) {
42+ return true ;
43+ }
44+ }
45+ return false ;
46+ }
47+
48+ private boolean containsLowercase (String password ) {
49+ for (char c : password .toCharArray ()) {
50+ if (Character .isLowerCase (c )) {
51+ return true ;
52+ }
53+ }
54+ return false ;
55+ }
56+
57+ private boolean containsDigit (String password ) {
58+ for (char c : password .toCharArray ()) {
59+ if (Character .isDigit (c )) {
60+ return true ;
61+ }
62+ }
63+ return false ;
4164 }
4265}
You can’t perform that action at this time.
0 commit comments