36
36
import javax .swing .text .html .parser .ParserDelegator ;
37
37
38
38
import org .apache .commons .io .IOUtils ;
39
- import org .apache .commons .lang3 .StringEscapeUtils ;
39
+ import org .apache .commons .text .StringEscapeUtils ;
40
40
import org .owasp .html .PolicyFactory ;
41
41
import org .owasp .html .Sanitizers ;
42
42
import system .proxies .FileDocument ;
@@ -48,7 +48,8 @@ public class StringUtils {
48
48
private static final String UPPERCASE_ALPHA = stringRange ('A' , 'Z' );
49
49
private static final String LOWERCASE_ALPHA = stringRange ('a' , 'z' );
50
50
private static final String DIGITS = stringRange ('0' , '9' );
51
- private static final String SPECIAL = stringRange ('!' , '/' );
51
+ // Used in tests as well
52
+ static final String SPECIAL = stringRange ('!' , '/' );
52
53
private static final String ALPHANUMERIC = UPPERCASE_ALPHA + LOWERCASE_ALPHA + DIGITS ;
53
54
54
55
static final Map <String , PolicyFactory > SANITIZER_POLICIES =
@@ -279,39 +280,63 @@ public void handleEndTag(HTML.Tag tag, int pos) {
279
280
}
280
281
281
282
/**
282
- * Returns a random strong password containing a specified minimum number of digits, uppercase
283
- * and special characters.
283
+ * Returns a random strong password containing a specified minimum number of uppercase, digits
284
+ * and the exact number of special characters.
284
285
*
285
286
* @param minLen Minimum length
286
287
* @param maxLen Maximum length
287
- * @param noOfCAPSAlpha Number of capitals
288
- * @param noOfDigits Number of digits
289
- * @param noOfSplChars Number of special characters
290
- * @return
288
+ * @param noOfCAPSAlpha Minimum number of capitals
289
+ * @param noOfDigits Minimum number of digits
290
+ * @param noOfSplChars Exact number of special characters
291
+ * @deprecated Use the overload randomStrongPassword instead
291
292
*/
293
+ @ Deprecated
292
294
public static String randomStrongPassword (int minLen , int maxLen , int noOfCAPSAlpha , int noOfDigits , int noOfSplChars ) {
293
295
if (minLen > maxLen ) {
294
296
throw new IllegalArgumentException ("Min. Length > Max. Length!" );
295
297
}
296
298
if ((noOfCAPSAlpha + noOfDigits + noOfSplChars ) > minLen ) {
297
299
throw new IllegalArgumentException ("Min. Length should be atleast sum of (CAPS, DIGITS, SPL CHARS) Length!" );
298
300
}
299
- return generateCommonLangPassword (minLen , maxLen , noOfCAPSAlpha , noOfDigits , noOfSplChars );
301
+ return generateCommonLangPassword (minLen , maxLen , noOfCAPSAlpha , 0 , noOfDigits , noOfSplChars );
302
+ }
303
+
304
+ /**
305
+ * Returns a random strong password containing a specified minimum number of uppercase, lowercase, digits
306
+ * and the exact number of special characters.
307
+ *
308
+ * @param minLen Minimum length
309
+ * @param maxLen Maximum length
310
+ * @param noOfCAPSAlpha Minimum number of capitals
311
+ * @param noOfLowercaseAlpha Minimum number of lowercase letters
312
+ * @param noOfDigits Minimum number of digits
313
+ * @param noOfSplChars Exact number of special characters
314
+ */
315
+ public static String randomStrongPassword (int minLen , int maxLen , int noOfCAPSAlpha , int noOfLowercaseAlpha , int noOfDigits , int noOfSplChars ) {
316
+ if (minLen > maxLen ) {
317
+ throw new IllegalArgumentException ("Min. Length > Max. Length!" );
318
+ }
319
+ if ((noOfCAPSAlpha + noOfLowercaseAlpha + noOfDigits + noOfSplChars ) > minLen ) {
320
+ throw new IllegalArgumentException ("Min. Length should be atleast sum of (CAPS, LOWER, DIGITS, SPL CHARS) Length!" );
321
+ }
322
+ return generateCommonLangPassword (minLen , maxLen , noOfCAPSAlpha , noOfLowercaseAlpha , noOfDigits , noOfSplChars );
300
323
}
301
324
302
325
// See https://www.baeldung.com/java-generate-secure-password
303
326
// Implementation inspired by https://github.com/eugenp/tutorials/tree/master/core-java-modules/core-java-string-apis (under MIT license)
304
- private static String generateCommonLangPassword (int minLen , int maxLen , int noOfCapsAlpha , int noOfDigits , int noOfSplChars ) {
327
+ private static String generateCommonLangPassword (int minLen , int maxLen , int noOfCapsAlpha , int noOfLowercaseAlpha , int noOfDigits , int noOfSplChars ) {
305
328
String upperCaseLetters = randomStringFromCharArray (noOfCapsAlpha , UPPERCASE_ALPHA .toCharArray ());
329
+ String lowerCaseLetters = randomStringFromCharArray (noOfLowercaseAlpha , LOWERCASE_ALPHA .toCharArray ());
306
330
String numbers = randomStringFromCharArray (noOfDigits , DIGITS .toCharArray ());
307
331
String specialChar = randomStringFromCharArray (noOfSplChars , SPECIAL .toCharArray ());
308
332
309
- final int fixedNumber = noOfCapsAlpha + noOfDigits + noOfSplChars ;
333
+ final int fixedNumber = noOfCapsAlpha + noOfLowercaseAlpha + noOfDigits + noOfSplChars ;
310
334
final int lowerBound = minLen - fixedNumber ;
311
335
final int upperBound = maxLen - fixedNumber ;
312
336
String totalChars = randomStringFromCharArray (lowerBound , upperBound , ALPHANUMERIC .toCharArray ());
313
337
314
338
String combinedChars = upperCaseLetters
339
+ .concat (lowerCaseLetters )
315
340
.concat (numbers )
316
341
.concat (specialChar )
317
342
.concat (totalChars );
0 commit comments