-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPasswordValidator.java
72 lines (64 loc) · 2.31 KB
/
PasswordValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* $Header$
*
* Copyright (C) 2019 Cefalo AS.
* All Rights Reserved. No use, copying or distribution of this
* work may be made except in accordance with a valid license
* agreement from Cefalo AS. This notice must be included on all
* copies, modifications and derivatives of this work.
*/
package com.cefalo.tdd;
import java.util.Arrays;
import java.util.List;
/**
* @author <a href="mailto:fmshaon@gmail.com">Ferdous Mahmud Shaon</a>
* @author last modified by $Author$
* @version $Revision$ $Date$
*/
public class PasswordValidator {
/*
* Definition of valid password
* - Min Length: 6 characters
* - Max Length: 12 characters
* - Must have at least 1 lowercase character
* - Must have at least 1 uppercase character
* - Must have at least 1 numeric character
* - Must have at least 1 of these special characters (!, @, #, $, %, ^, &)
*/
public static boolean isValidPassword(final String pPassword) {
if(pPassword.length()<6 || pPassword.length()>12) {
return false;
}
boolean hasLowerCaseCharacter = false;
boolean hasUpperCaseCharacter = false;
boolean hasNumericCharacter = false;
boolean hasSpecialCharacter = false;
final List<Character> specialCharacters = Arrays.asList('!', '@', '#', '$', '%', '^', '&');
for(int i=0;i<pPassword.length();i++) {
char currentChar = pPassword.charAt(i);
if(Character.isLowerCase(currentChar) && !hasLowerCaseCharacter) {
hasLowerCaseCharacter = true;
}
if(Character.isUpperCase(currentChar) && !hasUpperCaseCharacter) {
hasUpperCaseCharacter = true;
}
if(Character.isDigit(currentChar) && !hasNumericCharacter) {
hasNumericCharacter = true;
}
/* character is not lower / uppercase character, not digit */
if(!Character.isLowerCase(currentChar) && !Character.isUpperCase(currentChar) && !Character.isDigit(currentChar)) {
if(specialCharacters.contains(currentChar) && !hasSpecialCharacter) {
hasSpecialCharacter = true;
}
else {
/* current character is not an allowed special character */
return false;
}
}
if(hasLowerCaseCharacter && hasUpperCaseCharacter && hasNumericCharacter && hasSpecialCharacter) {
return true;
}
}
return false;
}
}