1
13693261870
2024-12-08 51fa0405c43598cfc4ae2a4603b12cbd2d57745b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.se.system.utils;
 
@SuppressWarnings("ALL")
public class CheckPwdUtils {
    /**
     * NUM 数字
     * SMALL_LETTER 小写字母
     * CAPITAL_LETTER 大写字母
     * OTHER_CHAR 特殊字符
     */
    private static final int NUM = 1;
 
    private static final int SMALL_LETTER = 2;
 
    private static final int CAPITAL_LETTER = 3;
 
    private static final int OTHER_CHAR = 4;
 
    /**
     * 检查密码的强度
     *
     * @param passwd 密码
     * @return 密码等级
     */
    public static int checkPwdLevel(String passwd) {
        if (null == passwd) {
            throw new IllegalArgumentException("密码不能为空");
        }
        if (passwd.length() < 8) {
            return 1;
        }
 
        int level = 0;
        // 判断密码长度是否大于等于8 是level++
        if (passwd.length() >= 8) {
            level++;
        }
        // 判断密码是否含有数字 有level++
        if (countLetter(passwd, NUM) > 0) {
            level++;
        }
        // 判断密码是否含有小写字母 有level++
        if (countLetter(passwd, SMALL_LETTER) > 0) {
            level++;
        }
        // 判断密码是否还有大写字母 有level++
        if (countLetter(passwd, CAPITAL_LETTER) > 0) {
            level++;
        }
        // 判断密码是否还有特殊字符 有level++
        if (countLetter(passwd, OTHER_CHAR) > 0) {
            level++;
        }
 
        return level;
    }
 
    /**
     * 计算密码中指定字符类型的数量
     *
     * @param passwd 密码
     * @param type   类型
     * @return 数量
     */
    private static int countLetter(String passwd, int type) {
        int count = 0;
        if (null != passwd && !passwd.isEmpty()) {
            for (char c : passwd.toCharArray()) {
                if (checkCharacterType(c) == type) {
                    count++;
                }
            }
        }
        return count;
    }
 
    /**
     * 检查字符类型,包括num、大写字母、小写字母和其他字符。
     *
     * @param c – 字符
     * @return 类型
     */
    private static int checkCharacterType(char c) {
        if (c >= 48 && c <= 57) {
            return NUM;
        }
        if (c >= 65 && c <= 90) {
            return CAPITAL_LETTER;
        }
        if (c >= 97 && c <= 122) {
            return SMALL_LETTER;
        }
 
        return OTHER_CHAR;
    }
}