如何用三维数组实现键盘弱密码的校验
本篇文章为大家展示了如何用三维数组实现键盘弱密码的校验,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
裕民网站建设公司成都创新互联公司,裕民网站设计制作,有大型网站制作公司丰富经验。已为裕民成百上千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的裕民做网站的公司定做!
一个键盘弱密码简单校验实现,其中连续3个字符校验的逻辑是先获取当前字符定位下标,通过下标+-1获取下一个字符和下下个字符与字符串接下来的两个字符做比较。
package com.linya.common.util;
import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set;
import cn.hutool.core.util.ChatUtil;
/**
@author 临涯掠川
@Description 键盘密码复杂度校验工具
@create 2021-03-16 15:21 */
public class TextValidUtil {
private static char[][][] KEYBOARD = new char[5][15][2]; static { KEYBOARD[0][0][0] = '`';KEYBOARD[0][0][1] = '~';KEYBOARD[1][1][0] = 'q';KEYBOARD[1][1][1] = 'Q'; KEYBOARD[0][1][0] = '1';KEYBOARD[0][1][1] = '!';KEYBOARD[1][2][0] = 'w';KEYBOARD[1][2][1] = 'W'; KEYBOARD[0][2][0] = '2';KEYBOARD[0][2][1] = '@';KEYBOARD[1][3][0] = 'e';KEYBOARD[1][3][1] = 'E'; KEYBOARD[0][3][0] = '3';KEYBOARD[0][3][1] = '#';KEYBOARD[1][4][0] = 'r';KEYBOARD[1][4][1] = 'R'; KEYBOARD[0][4][0] = '4';KEYBOARD[0][4][1] = '$';KEYBOARD[1][5][0] = 't';KEYBOARD[1][5][1] = 'T'; KEYBOARD[0][5][0] = '5';KEYBOARD[0][5][1] = '%';KEYBOARD[1][6][0] = 'y';KEYBOARD[1][6][1] = 'Y'; KEYBOARD[0][6][0] = '6';KEYBOARD[0][6][1] = '^';KEYBOARD[1][7][0] = 'u';KEYBOARD[1][7][1] = 'U'; KEYBOARD[0][7][0] = '7';KEYBOARD[0][7][1] = '&';KEYBOARD[1][8][0] = 'i';KEYBOARD[1][8][1] = 'I'; KEYBOARD[0][8][0] = '8';KEYBOARD[0][8][1] = '*';KEYBOARD[1][9][0] = 'o';KEYBOARD[1][9][1] = 'O'; KEYBOARD[0][9][0] = '9';KEYBOARD[0][9][1] = '(';KEYBOARD[1][10][0] = 'p';KEYBOARD[1][10][1] = 'P'; KEYBOARD[0][10][0] = '0';KEYBOARD[0][10][1] = ')';KEYBOARD[1][11][0] = '[';KEYBOARD[1][11][1] = '{'; KEYBOARD[0][11][0] = '-';KEYBOARD[0][11][1] = '_';KEYBOARD[1][12][0] = ']';KEYBOARD[1][12][1] = '}'; KEYBOARD[0][12][0] = '=';KEYBOARD[0][12][1] = '+';KEYBOARD[1][13][0] = '\\';KEYBOARD[1][13][1] = '|'; KEYBOARD[2][1][0] = 'a';KEYBOARD[2][1][1] = 'A';KEYBOARD[3][1][0] = 'z';KEYBOARD[3][1][1] = 'Z'; KEYBOARD[2][2][0] = 's';KEYBOARD[2][2][1] = 'S';KEYBOARD[3][2][0] = 'x';KEYBOARD[3][2][1] = 'X'; KEYBOARD[2][3][0] = 'd';KEYBOARD[2][3][1] = 'D';KEYBOARD[3][3][0] = 'c';KEYBOARD[3][3][1] = 'C'; KEYBOARD[2][4][0] = 'f';KEYBOARD[2][4][1] = 'F';KEYBOARD[3][4][0] = 'v';KEYBOARD[3][4][1] = 'V'; KEYBOARD[2][5][0] = 'g';KEYBOARD[2][5][1] = 'G';KEYBOARD[3][5][0] = 'b';KEYBOARD[3][5][1] = 'B'; KEYBOARD[2][6][0] = 'h';KEYBOARD[2][6][1] = 'H';KEYBOARD[3][6][0] = 'n';KEYBOARD[3][6][1] = 'N'; KEYBOARD[2][7][0] = 'j';KEYBOARD[2][7][1] = 'J';KEYBOARD[3][7][0] = 'm';KEYBOARD[3][7][1] = 'M'; KEYBOARD[2][8][0] = 'k';KEYBOARD[2][8][1] = 'K';KEYBOARD[3][8][0] = ',';KEYBOARD[3][8][1] = '<'; KEYBOARD[2][9][0] = 'l';KEYBOARD[2][9][1] = 'L';KEYBOARD[3][9][0] = '.';KEYBOARD[3][9][1] = '>'; KEYBOARD[2][10][0] = ';';KEYBOARD[2][10][1] = ':';KEYBOARD[3][10][0] = '/';KEYBOARD[3][10][1] = '?'; KEYBOARD[2][11][0] = '\'';KEYBOARD[2][11][1] = '"'; KEYBOARD[4][14][0] = ' '; } private static ListEXCLUDED_WORD = Arrays.asList("admin", "root"); private final static int MIN_SIZE = 8; private final static int MAX_SIZE = 32; /** * [@param](https://my.oschina.net/u/2303379) account * [@param](https://my.oschina.net/u/2303379) passWord * @return * @Description 弱口令校验 * @rule * a. 最小8位,最大32位 * b. 大写、小写、数字、特殊字符,至少包含三种 * c. 不允许出现3位以上相同的字符和不允许出现键盘相邻的同方向3位以上连续字符 * d. 不允许密码包含账号全称 * e. 不允许出现常见单词,如admin、root等 */
public static boolean isWeakPassWord(String account, String passWord) {
// 长度校验 if (passWord.length() < 8 || passWord.length() > 32) return true; // 常见单词校验 if (EXCLUDED_WORD.contains(passWord)) return true; // 名称校验 if (account != null && passWord.contains(account)) return true; // 3个字符和字符种类校验 if (isThirdCharsRule(passWord)) return true; return false; } /** * @param characters * @return * @rule * 1.字符种类不少于3类 * 2.连续3个字符检查 * 3.键盘临近3个字符检查 */ private static boolean isThirdCharsRule(String characters) { char[] chars = characters.toCharArray(); Settype = new HashSet<>(); for (int i = chars.length - 1; i >= 2; i--) { if (chars[i] == chars[i - 1] && chars[i - 1] == chars[i - 2]) return true; int[] firstIndex = getIndex(chars[i]); if (isThirdCharByIndex(firstIndex, chars[i - 1], chars[i - 2])) return true; saveCharType(chars[i], type); } saveCharType(chars[0], type); saveCharType(chars[1], type); if (type.size() < 3) return true; return false; } private static int[] getIndex(char cha) { int[] index = new int[3]; for (int i = 0; i < KEYBOARD.length; i++) { for (int j = 0; j < KEYBOARD[i].length; j++) { for (int k = 0; k < KEYBOARD[i][j].length; k++) { if (KEYBOARD[i][j][k] == cha) { index[0] = i; index[1] = j; index[2] = k; return index; } } } } throw new RuntimeException("exist unrecognized character:" + cha); } private static boolean isThirdCharByIndex(int[] index, char secondChar, char thirdChar) { if (orientedCheck(index, index[0], 1, 0, 0, KEYBOARD.length, secondChar, thirdChar)) return true; if (orientedCheck(index, index[1], 0, 1, 0, KEYBOARD[0].length, secondChar, thirdChar)) return true; if (orientedCheck(index, index[2], 0, 0, 1, KEYBOARD[0][0].length, secondChar, thirdChar)) return true; return false; } private static boolean orientedCheck(int[] index, int validIndex, int first, int second, int third, int size, char secondChar, char thirdChar) { if (validIndex + 2 < size) if (nearCharCompare(index, first, second, third, secondChar, thirdChar)) return true; if (validIndex - 1 > 0) if (nearCharCompare(index, -first, -second, -third, secondChar, thirdChar)) return true; return false; } private static boolean nearCharCompare(int[] index, int first, int second, int third, char secondChar, char thirdChar) { if (KEYBOARD[index[0] + first][index[1] + second][index[2] + third] == secondChar && KEYBOARD[index[0] + first * 2][index[1] + second * 2][index[2] + third * 2] == thirdChar) return true; return false; } private static void saveCharType(char cha, Set type){ if (CharUtil.isLetterLower(cha)) type.add(1); else if (CharUtil.isLetterUpper(cha)) type.add(2); else if (CharUtil.isNumber(cha)) type.add(3); else type.add(4); } public static void main(String[] args) { String account = "admin"; String passWord = "1qrfw3j6tesedex@0!?| 上述内容就是如何用三维数组实现键盘弱密码的校验,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
分享题目:如何用三维数组实现键盘弱密码的校验
标题来源:http://pwwzsj.com/article/pgpoii.html