-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathpassutil.js
148 lines (137 loc) · 4.56 KB
/
passutil.js
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
var securePassword = function(length) {
var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var retVal = "";
for (var i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
return retVal;
};
var isLucky = function() {
return Math.floor(Math.random() * 100)%2==0;
};
var getRandomInt = function(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
};
var ensureMinLength = function(inputString) {
var minLength = 10;
if(inputString.length < minLength) {
var randomString = securePassword(minLength - inputString.length);
if(isLucky()){
inputString = inputString + randomString;
} else {
inputString = randomString + inputString;
}
}
return inputString;
};
var securePasswordFromInputString = function(inputString){
const result = {};
if(inputString) {
inputString = inputString.replace(/\s/g, "");
result.randomReplaceResult = randomReplace(inputString);
inputString = result.randomReplaceResult.charArray;
inputString = ensureMinLength(inputString);
}
result.suggestion = inputString;
return result;
};
var scorePassword = function(pass) {
var score = 0;
if (!pass)
return score;
// award every unique letter until 5 repetitions
var letters = new Object();
for (var i=0; i<pass.length; i++) {
letters[pass[i]] = (letters[pass[i]] || 0) + 1;
score += 5.0 / letters[pass[i]];
}
// bonus points for mixing it up
var variations = {
digits: /\d/.test(pass),
lower: /[a-z]/.test(pass),
upper: /[A-Z]/.test(pass),
nonWords: /\W/.test(pass),
}
variationCount = 0;
for (var check in variations) {
variationCount += (variations[check] == true) ? 1 : 0;
}
score += (variationCount - 1) * 10;
return parseInt(score);
};
var checkPassStrength = function (pass) {
var score = scorePassword(pass);
if (score > 80)
return "<span class='label label-success'> Strong </span>";
if (score > 60)
return "<span class='label label-warning'> Good </span>";
if (score >= 30)
return "<span class='label label-danger'> Weak </span>";
return "";
};
var memorableCharMap = new Map();
memorableCharMap.set("a",["a","A","@","4"]);
memorableCharMap.set("b",["b","B","6","8"]);
memorableCharMap.set("c",["c","C"]);
memorableCharMap.set("d",["d","D"]);
memorableCharMap.set("e",["e","E","3"]);
memorableCharMap.set("f",["f","F"]);
memorableCharMap.set("g",["g","G"]);
memorableCharMap.set("h",["h","H","#"]);
memorableCharMap.set("i",["i","I","1","!"]);
memorableCharMap.set("j",["j","J"]);
memorableCharMap.set("k",["k","K"]);
memorableCharMap.set("l",["l","L"]);
memorableCharMap.set("m",["m","M","w","W"]);
memorableCharMap.set("n",["n","N"]);
memorableCharMap.set("o",["o","O","0","."]);
memorableCharMap.set("p",["p","P"]);
memorableCharMap.set("q",["q","Q","0","9"]);
memorableCharMap.set("r",["r","R"]);
memorableCharMap.set("s",["s","S","5","$"]);
memorableCharMap.set("t",["t","T","7"]);
memorableCharMap.set("u",["u","U"]);
memorableCharMap.set("v",["v","V","<",">"]);
memorableCharMap.set("w",["w","W","m","M"]);
memorableCharMap.set("x",["x","X","*"]);
memorableCharMap.set("y",["y","Y"]);
memorableCharMap.set("z",["z","Z","2"]);
memorableCharMap.set("0",["o","O","0","."]);
memorableCharMap.set("i",["i","I","1","!"]);
memorableCharMap.set("z",["z","Z","2"]);
memorableCharMap.set("e",["e","E","3"]);
memorableCharMap.set("4",["a","A","@","4"]);
memorableCharMap.set("s",["s","S","5","$"]);
memorableCharMap.set("8",["b","B","6","8"]);
memorableCharMap.set("t",["t","T","7"]);
memorableCharMap.set("b",["b","B","6","8"]);
memorableCharMap.set("q",["q","Q","0","9"]);
var getRandomCharFromMap = function(inputchar) {
var arr = memorableCharMap.get(inputchar);
if(arr) {
var newChar = arr[getRandomInt(0,arr.length-1)];
if(newChar) {
return newChar;
}
}
return inputchar;
};
var randomReplace = function(inputString) {
const result = {};
var charArray = inputString.split('');
result.requestLevelCharMappings = new Map();
let chountChanged = 0;
for (i = 0; i < charArray.length; i++){
let toChar = result.requestLevelCharMappings.get(charArray[i]);
if(!toChar) {
toChar = (chountChanged > 2) ? charArray[i] : getRandomCharFromMap(charArray[i]);
result.requestLevelCharMappings.set(charArray[i], toChar);
}
if(toChar !== charArray[i]) {
chountChanged++;
}
charArray[i] = toChar;
}
result.charArray = charArray.join("");
return result;
}