pwd-strength.js
1.49 KB
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
/**
* @desc: 计算密码复杂度(from:http://www.oschina.net/code/snippet_127301_17269)
* @author: xuqi(qi.xu@yoho.cn)
* @date: 2015/5/6
*/
function computeComplex(password) {
var complex = 0;
var length = password.length;
var pre = '';
var preType = 0;
for (var i = 0; i < length; i++) {
var cur = password.charAt(i);
var curType = gettype(password, i);
if (preType != curType || !isregular(cur, pre, curType)) {
complex += curType + getcomplex(curType, preType);
}
pre = cur;
preType = curType;
}
return complex;
}
function gettype(str, i) {
if (str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57) {
return 1;
}
else if(str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) {
return 2;
}
else if(str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {
return 3;
}
return 4;
}
function isregular(cur, pre, type) {
var curCode = cur.charCodeAt(0);
var preCode = pre.charCodeAt(0);
if(curCode - preCode == 0){
return true;
}
if(type != 4 && (curCode - preCode == 1 || curCode - preCode == -1)){
return true;
}
return false;
}
function getcomplex(curType, preType){
if(preType == 0 || curType == preType){
return 0;
}else if(curType == 4 || preType == 4){
return 2;
}else{
return 1;
}
}
module.exports = computeComplex;