您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何用三維數(shù)組實現(xiàn)鍵盤弱密碼的校驗,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
一個鍵盤弱密碼簡單校驗實現(xiàn),其中連續(xù)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 List<String> EXCLUDED_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. 大寫、小寫、數(shù)字、特殊字符,至少包含三種 * c. 不允許出現(xiàn)3位以上相同的字符和不允許出現(xiàn)鍵盤相鄰的同方向3位以上連續(xù)字符 * d. 不允許密碼包含賬號全稱 * e. 不允許出現(xiàn)常見單詞,如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.連續(xù)3個字符檢查 * 3.鍵盤臨近3個字符檢查 */ private static boolean isThirdCharsRule(String characters) { char[] chars = characters.toCharArray(); Set<Integer> type = 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<Integer> 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!?|<czqz4rF"; boolean isWeak = isWeakPassWord(account, passWord); System.out.println(isWeak); } }
上述內容就是如何用三維數(shù)組實現(xiàn)鍵盤弱密碼的校驗,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。