Java
자바 비밀번호 생성(금융보안원)
by NaHyungMin
2022. 5. 10.
import java.util.ArrayList;
import java.util.List;
public class PasswordValidation {
//220510 nhm,
public static boolean isPasswordInvalid(String password, String email, String birth, String cellNo, int characterCheckCount) {
//비밀번호 길이
if(!isPasswordRange(password)) {
return false;
}
//알파벳 대문자와 소문자, 숫자, 특수문자 중 두 종류 이상 포함
if(!isContainsPasswordType(password)) {
return false;
}
//계정명· 고유식별번호·생년월일·전화번호 포함 제한
if(!isContainUniqueNumber(password, email, birth, cellNo)) {
return false;
}
//4자리 이상 연속된 숫자·문자· 특수문자(!,@,#,$ 등) 포함 제한,
if(!isContinuedCharacter(password, characterCheckCount)) {
return false;
}
//4자리 이상 동일한 숫자·문자·특수문자 포함 제한
return isCharacterContainsCount(password, characterCheckCount);
}
private static boolean isPasswordRange(String password) {
final int passWordLength = 8;
//비밀번호 길이 체크
return passWordLength <= password.length();
}
private static boolean isContainsPasswordType(String password) {
//알파벳 대문자와 소문자, 숫자, 특수문자 중 두 종류 이상 포함
int passwordLength = password.length();
int passwordTypeCount = 0;
//"^[0-9a-zA-Z가-힣]*$";
final String upperEnglish = "[A-Z]";
final String lowerEnglish = "[a-z]";
final String numberCharacter = "[0-9]";
final String specialCharacter = "[a-zA-Z0-9 ㄱ-ㅎㅏ-ㅣ가-힣]";
String upper = replaceExpression(password, upperEnglish);
String lower = replaceExpression(password, lowerEnglish);
String number = replaceExpression(password, numberCharacter);
String special = replaceExpression(password, specialCharacter);
if(passwordLength > upper.length()) {
passwordTypeCount++;
}
if(passwordLength > lower.length()) {
passwordTypeCount++;
}
if(passwordLength > number.length()) {
passwordTypeCount++;
}
if(special.length() > 0) {
passwordTypeCount++;
}
return passwordTypeCount > 1;
}
private static String replaceExpression(String password, String pattern) {
return password.replaceAll(pattern, "");
}
private static boolean isContainUniqueNumber(String password, String email, String birth, String cellNo) {
final String specialCharacter = "[^0-9a-zA-Z]";
//이메일
String userEmail = replaceExpression(email.substring(0, email.indexOf("@")), specialCharacter);
if(password.contains(userEmail)) {
return false;
}
//생년월일
String userBirth = birth.substring(2);
if(password.contains(userBirth)) {
return false;
}
//전화번호
String[] cellNoArray = getCellNoArray(cellNo);
for (String checkCellNo : cellNoArray) {
if(password.contains(checkCellNo)) {
return false;
}
}
return true;
}
private static String[] getCellNoArray(String cellNo) {
final String cellNoRange = "01012345678";
return cellNo.length() == cellNoRange.length()
? new String[] { cellNo.substring(3, 7), cellNo.substring(7, 11) }
: new String[] { cellNo.substring(3, 6), cellNo.substring(7, 10) };
}
private static boolean isContinuedCharacter(String password, int continueCount) {
int nowChar = 0;
int charCount = 0;
for(char p : password.toCharArray()) {
if(nowChar != p) {
charCount = 1;
} else {
charCount++;
}
if(charCount > 3) {
break;
}
nowChar = p;
}
return continueCount > charCount;
}
//4자리 이상 동일한 숫자·문자·특수문자 포함 제한
private static boolean isCharacterContainsCount(String password, int containsCount) {
List<Character> characterList = new ArrayList<>();
for(char s : password.toCharArray()) {
if(characterList.contains(s)) {
continue;
}
characterList.add(s);
int arrayContainsCount = (int) password.chars().filter(x -> x == s).count();
if(arrayContainsCount >= containsCount) {
return false;
}
}
return true;
}
}