/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.commoncore.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * 强密码校验：包含数字、大小写字母和特殊符号，长度要求8-15位且不与用户名相同
 * @author hxl
 */
@SuppressWarnings("all")
public class CheckPwdUtil {

    // 数字
    public static final String REG_NUMBER = ".*\\d+.*";
    // 小写字母
    public static final String REG_UPPERCASE = ".*[A-Z]+.*";
    // 大写字母
    public static final String REG_LOWERCASE = ".*[a-z]+.*";
    // 特殊符号(~!@#$%^&*()_+|<>,.?/:;'[]{}\)
    public static final String REG_SYMBOL = ".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*";


    public static boolean checkPasswordRule(String username, String password) {
        if (StringUtils.isBlank(password) || password.length() < 8 || password.length() > 15) {
            return false;
        }

        int i = 0;
        if (password.matches(REG_NUMBER)) { i++; }
        if (password.matches(REG_LOWERCASE)) { i++; }
        if (password.matches(REG_UPPERCASE)) { i++; }
        if (password.matches(REG_SYMBOL)) { i++; }

        boolean contains = password.equals(username);
        if (i  < 4 || contains) {
            return false;
        }

        return true;
    }
}
