111
hxb
2022-11-24 0a3e07f10937484145f33c7560607b4b2353cb81
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.mm.android.deviceaddmodule.mobilecommon.utils;
 
import android.content.Context;
import android.text.TextUtils;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class PasswordCheckRules {
 
    private final static int AWARD_2 = 2;
    private final static int AWARD_3 = 3;
    private final static int AWARD_5 = 5;
 
    public static final int PASSWORD_INVALID = 59999;                            //密码为空 不合法
    public static final int PASSWORD_VALID_OK = 60000;                 //密码符合规则
    public static final int PASSWORD_NOT_MATCH = 60001;                //新密码和确认密码不一致
    public static final int PASSWORD_INVALID_COMBINATION = 60002;                   //新密码组合不合法(2种以上类型)
    public static final int PASSWORD_INVALID_LENGTH = 60003;                //新密码的长度不合法(8~32位)
    public static final int PASSWORD_INVALID_NO_SN = 60004;                //新密码不合法:不允许为序列号
    public static final int PASSWORD_NOT_SAFY = 60005;                //密码不符合安全基线要求
 
    /**
     * 自定义加密密码校验
     * @param pwd
     * @return
     */
    public static int checkSinglePasswordValidationLC( String pwd) {
        if (TextUtils.isEmpty(pwd)) return PASSWORD_INVALID;
 
        if (pwd.length() < 6 || pwd.length() > 32) return PASSWORD_INVALID_LENGTH;
 
        return PASSWORD_VALID_OK;
    }
 
    public static int checkSinglePasswordValidation(Context context, String pwd) {
        if (TextUtils.isEmpty(pwd)) return PASSWORD_INVALID;
 
        if (pwd.length() < 8 || pwd.length() > 32) return PASSWORD_INVALID_LENGTH;
 
        if (!isMultiCharacterPassword(pwd)) return PASSWORD_INVALID_COMBINATION;
 
        if(StringUtils.checkSafetyBaseline(null , pwd, context)) return PASSWORD_NOT_SAFY;
 
        return PASSWORD_VALID_OK;
    }
 
    //密码为8~32位字母且字符组合
    public static int checkPasswordValidation(String pwd, String conformPwd, Context context){
 
        if(TextUtils.isEmpty(pwd) || TextUtils.isEmpty(conformPwd)) return PASSWORD_INVALID;
 
        if (pwd.length() < 8 || pwd.length() > 32) return PASSWORD_INVALID_LENGTH;
 
        if(!isMultiCharacterPassword(pwd)) return PASSWORD_INVALID_COMBINATION;
 
        if (!pwd.equals(conformPwd)) return PASSWORD_NOT_MATCH;
 
        if(StringUtils.checkSafetyBaseline(null , pwd, context)) return PASSWORD_NOT_SAFY;
 
        return PASSWORD_VALID_OK;
 
    }
 
    //蓝牙锁管理员密码重置 密码为6~12位字母且字符组合
    public static int checkLockPasswordValidation(String pwd, String conformPwd, Context context){
 
        if(TextUtils.isEmpty(pwd) || TextUtils.isEmpty(conformPwd)) return PASSWORD_INVALID;
 
        if (pwd.length() < 6 || pwd.length() > 12) return PASSWORD_INVALID_LENGTH;
 
        if (!pwd.equals(conformPwd)) return PASSWORD_NOT_MATCH;
 
        return PASSWORD_VALID_OK;
 
    }
 
 
 
    //是否包含其他特殊字符
    public static boolean isSpeCharacterPassword(String str) {
 
        //27个特殊符号ASC码:33、35、36、37、40、41、42、43、44、45、46、47、60、61、62、63、64、91、92、93、94、95、96、123、124、125、126
        String charEx = "[\\!\\#\\$\\%\\(\\)\\*\\+\\,\\-\\.\\/\\<\\=" +
                "\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~]*";
        Pattern pChar = Pattern.compile(charEx);
        Matcher mChar = pChar.matcher(str);
        if (mChar.matches()) {
            return false;
        }
 
        return true;
    }
 
 
    //判断密码是否包含两种以上字符
    public static boolean isMultiCharacterPassword(String str) {
        String numEx = "[0-9]*"; // 不能纯数字
        Pattern pNum = Pattern.compile(numEx);
        Matcher mNum = pNum.matcher(str);
        if (mNum.matches()) {
            return false;
        }
 
        String lowEx = "[a-z]*"; //不能纯小写英语
        Pattern pLow = Pattern.compile(lowEx);
        Matcher mLow = pLow.matcher(str);
        if (mLow.matches()) {
            return false;
        }
 
        String highEx = "[A-Z]*"; //不能纯大写英语
        Pattern pHigh = Pattern.compile(highEx);
        Matcher mHigh = pHigh.matcher(str);
        if (mHigh.matches()) {
            return false;
        }
 
        //27个特殊符号ASC码:33、35、36、37、40、41、42、43、44、45、46、47、60、61、62、63、64、91、92、93、94、95、96、123、124、125、126
        String charEx = "[\\!\\#\\$\\%\\(\\)\\*\\+\\,\\-\\.\\/\\<\\=" +
                "\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~]*";
        Pattern pChar = Pattern.compile(charEx);
        Matcher mChar = pChar.matcher(str);
        if (mChar.matches()) {
            return false;
        }
 
        return true;
    }
 
    /**
     * 获取密码强度分数
     * @param password
     * @return
     */
    public static int getPwdStrength(String password){
        //遍历计算字符串中的数字个数、大写字母的个数、小写字母个数、符号个数
        int strength = 0;
        int lowerCaseNum = 0;
        int upperCaseNum = 0;
        int digitNum = 0;
        for (int i = 0;i<password.length();i++){
            if(Character.isDigit(password.charAt(i))){
                digitNum++;
            }else if(Character.isLowerCase(password.charAt(i))){
                lowerCaseNum++;
            }else if(Character.isUpperCase(password.charAt(i))){
                upperCaseNum++;
            }
        }
        int  characterNum = password.length() - lowerCaseNum - upperCaseNum - digitNum;
 
        //密码长度
        if (password.length() <= 4)
            strength = 5;
        else if(password.length() <= 7)
            strength = 10;
        else if(password.length() >= 8)
            strength = 25;
 
        //字母
        if (!(lowerCaseNum == 0 && upperCaseNum == 0)){
            strength = strength + 10;
            if(lowerCaseNum > 0 && upperCaseNum > 0)
                strength = strength + 10;
        }
 
        //数字
        if (digitNum == 1)
            strength = strength + 10;
        else if (digitNum > 1)
            strength = strength + 20;
 
        //符号
        if (characterNum == 1)
            strength = strength + 10;
        else if (characterNum > 1)
            strength = strength + 25;
 
        int awardScore = getAwardScore(upperCaseNum, lowerCaseNum, digitNum, characterNum);
        strength += awardScore;
        return strength;
    }
 
    /**
     * 五:奖励规则则 检验字母和符号组合
     * 2 分: 两种字符类型组合
     * 3 分: 三种字符类型组合
     * 5 分: 四种字符类型组合
     */
    private static int getAwardScore(int upperCaseLetterCount, int lowerCaseLetterCount, int numberCount, int symbolCount) {
 
        upperCaseLetterCount = upperCaseLetterCount > 0 ? 1 : 0;
        lowerCaseLetterCount = lowerCaseLetterCount > 0 ? 1 : 0;
        numberCount = numberCount > 0 ? 1 : 0;
        symbolCount = symbolCount > 0 ? 1 : 0;
        switch (upperCaseLetterCount + lowerCaseLetterCount + numberCount + symbolCount) {
            case 2:
                return AWARD_2;
            case 3:
                return AWARD_3;
            case 4:
                return AWARD_5;
            default:
                return 0;
        }
 
    }
 
}