package com.ruoyi.common.utils;

import java.util.regex.Pattern;

/**
 * 密码强度校验工具
 */
public class PasswordStrengthUtils
{
    public static final int STRONG_PASSWORD_MIN_LENGTH = 8;

    public static final int STRONG_PASSWORD_MAX_LENGTH = 20;

    public static final String STRONG_PASSWORD_MESSAGE = "新密码必须为8到20位，并同时包含大写字母、小写字母、数字和特殊字符，且不能包含空格或字符：< > \" ' \\ |";

    private static final Pattern UPPER_CASE_PATTERN = Pattern.compile("[A-Z]");

    private static final Pattern LOWER_CASE_PATTERN = Pattern.compile("[a-z]");

    private static final Pattern DIGIT_PATTERN = Pattern.compile("\\d");

    private static final Pattern SPECIAL_CHAR_PATTERN = Pattern.compile("[^A-Za-z0-9\\s<>\"'|\\\\]");

    private static final Pattern ILLEGAL_CHAR_PATTERN = Pattern.compile("[\\s<>\"'|\\\\]");

    private PasswordStrengthUtils()
    {
    }

    public static boolean isStrongPassword(String password)
    {
        if (StringUtils.isEmpty(password))
        {
            return false;
        }
        if (password.length() < STRONG_PASSWORD_MIN_LENGTH || password.length() > STRONG_PASSWORD_MAX_LENGTH)
        {
            return false;
        }
        if (ILLEGAL_CHAR_PATTERN.matcher(password).find())
        {
            return false;
        }
        return UPPER_CASE_PATTERN.matcher(password).find()
                && LOWER_CASE_PATTERN.matcher(password).find()
                && DIGIT_PATTERN.matcher(password).find()
                && SPECIAL_CHAR_PATTERN.matcher(password).find();
    }
}
