密码复杂性校验--包含用户名、键盘连续字符匹配
程序员文章站
2024-03-19 15:02:40
...
/**
* 验证密码-是否包含用户名字符(密码应与用户名无相关性,密码中不得包含用户名的完整字符串、大小写变位或形似变换的字符串)
*/
public static boolean verifyPasswordContainAccount(String password,String account){
boolean isContain = false;
if(!TextUtils.isEmpty(password) && !TextUtils.isEmpty(account)){
password = password.toLowerCase();
account = account.toLowerCase();
if(password.contains(account)){
return true;
}
String[] likes = {"a","l","o"};
String[] likeSign = {"@","!","0"};
String originalAccount = account+"";
for (int i = 0; i < likes.length; i++) {
String tempAccount = originalAccount.replace(likes[i],likeSign[i]);
account = account.replace(likes[i],likeSign[i]);
if(password.contains(tempAccount)||password.contains(account)){
return true;
}
}
}
return isContain;
}
/**
* 键盘连续字符统计4个
* @param str
* @return
*/
public static boolean isKeyBoardContinuousChar(String str){
boolean result = false;
char[][] c1 = {
{'!','@','#','$','%','^','&','*','(',')','_','+'},
{'q','w','e','r','t','y','u','i','o','p','{','}','|'},
{'a','s','d','f','g','h','j','k','l',':','"'},
{'z','x','c','v','b','n','m','<','>','?'}
};
char[][] c2 = {
{'1','2','3','4','5','6','7','8','9','0','-','='},
{'q','w','e','r','t','y','u','i','o','p','{','}','\\'},
{'a','s','d','f','g','h','j','k','l',';','\''},
{'z','x','c','v','b','n','m',',','.','/'}
};
// String[] strList = str.split("");
// //获取坐标位置
// String[] y ;
// String[] x ;
for (char[][] c: new char[][][]{c1,c2}) {
//横向
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[i].length-3; j++) {
//创建连续字符
StringBuffer sb = new StringBuffer();
for (int k = j; k < j+4; k++) {
sb.append(c[i][k]);
}
String keyStr = sb.toString();
if(str.contains(keyStr)){
return true;
}
}
}
//纵向
for (int i = 0; i < c[3].length; i++) {
//创建连续字符--每列只有4个
StringBuffer sb = new StringBuffer();
for (int j = 0; j < 4; j++) {
sb.append(c[j][i]);
}
String keyStr = sb.toString();
if(str.contains(keyStr)){
return true;
}
}
}
return result;
}
/**
* 密码验证
* (至少由8位及以上大写字母、小写字母、数字与特殊符号等4类中3类混合)
* @param password
* @return
*/
public static boolean checkPassword(String password) {
boolean flag = false;
try {
int c = 0;
if(find("[a-z]+",password)){
c++;
}
if(find("[A-Z]+",password)){
c++;
}
if(find("[0-9]+",password)){
c++;
}
if(find("\\W+|_",password) && !find("\\s+",password)){//特殊符号
c++;
}
if(c >= 3){
flag = true;
}
LogUtil.i("TAG","c:"+c);
} catch (Exception e) {
flag = false;
}
return flag;
}
//通用匹配
public static boolean find(String regexStr, String input){
boolean flag = false;
try {
Pattern regex = Pattern.compile(regexStr);
Matcher matcher = regex.matcher(input);
flag = matcher.find();//部分进行匹配
} catch (Exception e) {
flag = false;
}
return flag;
}