Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use 3D Array to verify Keyboard weak passwords

Shulou Source: shulou.com Published: 2022-06-03 14:27:07 09月29日 Update

This article shows you how to use a three-dimensional array to achieve weak password verification keyboard, concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article I hope you can gain something.

A keyboard weak password simple verification implementation, in which the logic of three consecutive character verification is to first obtain the current character positioning index, through the index +-1 to obtain the next character and the next character and the next two characters in the string to compare.

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 Linya Luichuan

@Description Keyboard Password Complexity Verification Tool

@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][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 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 * @ weak password check * @rule * a. Description Minimum 8 bits, maximum 32 bits * b. Uppercase, lowercase, numeric, special characters, at least three * c. It is not allowed to appear more than 3 characters of the same character and not allowed to appear more than 3 consecutive characters in the same direction adjacent to the keyboard * d. Password containing full account name * e. Common words such as admin, root, etc. */are not allowed

public static boolean isWeakPassWord(String account, String passWord) {

//Length check if (passWord.length()

< 8 || passWord.length() >

32) return true; //Common word check if (EXCLUDED_WORD.contains(passWord)) return true; //name check if (account != null && passWord.contains(account)) return true; // 3 characters and character type check if (isThirdCharsRule(passWord)) return true; return false;}/** * @param characters * @return * @rule * 1. Character types are not less than 3 * 2. Check for consecutive 3 characters * 3. Check for keyboard proximity to 3 characters */private static boolean isThirdCharsRule(String characters) { char[] chars = characters.toCharArray(); Set 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 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!?|

Tags: Characters keyboard password array 3D subscript content word common skill knowledge category check complex minimum special same concise next concise Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft Shulou Tech Info MariaDB vpn