Java工具集-密码检测工具

2020-06-10 10:10:12 浏览数 (1)

代码示例
代码语言:javascript复制
import java.util.regex.Pattern;

/**
 * @program: simple_tools
 * @description: 密码检测工具类
 * @author: Mr.chen
 * @create: 2020-05-18 11:26
 **/
public class PasswordUtils {

    /**
     * 判断是否为纯数字简单密码
     * @param pwd
     * @return 如 111111 123456 654321返回true
     */
    public static boolean isSimplePwd(String pwd) {
        if (null == pwd || "".equals(pwd.trim())) {
            return false;
        }
        if (!Pattern.compile("[0-9]*").matcher(pwd).matches()) {
            return false;
        }

        boolean flag1 = true;
        boolean flag2 = true;
        boolean flag3 = true;
        char[] chars = pwd.toCharArray();
        int iterations = chars.length - 1;
        for (int i = 0; i < iterations; i  ) {
            int num1 = Integer.parseInt(chars[i]   "");
            int num2 = Integer.parseInt(chars[i   1]   "");

            if (num1 != num2 - 1) {
                flag1 = false;
            }
            if (num1 != num2   1) {
                flag2 = false;
            }
            if (num1 != num2) {
                flag3 = false;
            }
            if (!(flag1 || flag2 || flag3)) {
                break;
            }
        }
        return (flag1 || flag2 || flag3);
    }

    /**
     *  检查密码合格性
     * @param pwd
     * @return 密码长度需8-32位且至少含数字、字母、字符中的两种 true
     */
    public static boolean pwdIsOk(String pwd) {
        if (null == pwd || "".equals(pwd.trim())) {
            return false;
        }
        String pattern = "^(?![a-zA-Z] $)(?![A-Z0-9] $)(?![A-Z\W_!@#$%^&*`~()- =] $)(?![a-z0-9] $)(?![a-z\W_!@#$%^&*`~()- =] $)(?![0-9\W_!@#$%^&*`~()- =] $)[a-zA-Z0-9\W_!@#$%^&*`~()- =]{8,32}$";
        return Pattern.compile(pattern).matcher(pwd).matches();
    }

    public static String checkPassword(String pwd) {
        String regexZ = "\d*";
        String regexS = "[a-zA-Z] ";
        String regexT = "\W $";
        String regexZT = "\D*";
        String regexST = "[\d\W]*";
        String regexZS = "\w*";
        String regexZST = "[\w\W]*";

        if (pwd.matches(regexZ)) {
            return "弱";
        }
        if (pwd.matches(regexS)) {
            return "弱";
        }
        if (pwd.matches(regexT)) {
            return "弱";
        }
        if (pwd.matches(regexZT)) {
            return "中";
        }
        if (pwd.matches(regexST)) {
            return "中";
        }
        if (pwd.matches(regexZS)) {
            return "中";
        }
        if (pwd.matches(regexZST)) {
            return "强";
        }
        return pwd;

    }

    public static void main(String[] args) {
        System.out.println(pwdIsOk("??..........13a"));
    }
}

0 人点赞