-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.min.js
332 lines (285 loc) · 9.74 KB
/
password.min.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
;(function($) {
'use strict';
var Password = function ($object, options) {
var defaults = {
enterPass: 'Type your password',
shortPass: 'The password is too short',
containsField: 'The password contains your username',
steps: {
13: 'Really insecure password',
33: 'Weak; try combining letters & numbers',
67: 'Medium; try using special characters',
94: 'Strong password',
},
showPercent: false,
showText: true,
animate: true,
animateSpeed: 'fast',
field: false,
fieldPartialMatch: true,
minimumLength: 4,
closestSelector: 'div',
useColorBarImage: false,
customColorBarRGB: {
red: [0, 240],
green: [0, 240],
blue: 10
},
};
options = $.extend({}, defaults, options);
/**
* Returns strings based on the score given.
*
* @param {int} score Score base.
* @return {string}
*/
function scoreText(score) {
if (score === -1) {
return options.shortPass;
}
if (score === -2) {
return options.containsField;
}
score = score < 0 ? 0 : score;
var text = options.shortPass;
var sortedStepKeys = Object.keys(options.steps).sort();
for (var step in sortedStepKeys) {
var stepVal = sortedStepKeys[step];
if (stepVal < score) {
text = options.steps[stepVal];
}
}
return text;
}
/**
* Returns a value between -2 and 100 to score
* the user's password.
*
* @param {string} password The password to be checked.
* @param {string} field The field set (if options.field).
* @return {int}
*/
function calculateScore(password, field) {
var score = 0;
// password < options.minimumLength
if (password.length < options.minimumLength) {
return -1;
}
if (options.field) {
// password === field
if (password.toLowerCase() === field.toLowerCase()) {
return -2;
}
// password contains field (and fieldPartialMatch is set to true)
if (options.fieldPartialMatch && field.length) {
var user = new RegExp(field.toLowerCase());
if (password.toLowerCase().match(user)) {
return -2;
}
}
}
// password length
score += password.length * 4;
score += checkRepetition(1, password).length - password.length;
score += checkRepetition(2, password).length - password.length;
score += checkRepetition(3, password).length - password.length;
score += checkRepetition(4, password).length - password.length;
// password has 3 numbers
if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) {
score += 5;
}
// password has at least 2 symbols
var symbols = '.*[!,@,#,$,%,^,&,*,?,_,~]';
symbols = new RegExp('(' + symbols + symbols + ')');
if (password.match(symbols)) {
score += 5;
}
// password has Upper and Lower chars
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) {
score += 10;
}
// password has number and chars
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) {
score += 15;
}
// password has number and symbol
if (password.match(/([!@#$%^&*?_~])/) && password.match(/([0-9])/)) {
score += 15;
}
// password has char and symbol
if (password.match(/([!@#$%^&*?_~])/) && password.match(/([a-zA-Z])/)) {
score += 15;
}
// password is just numbers or chars
if (password.match(/^\w+$/) || password.match(/^\d+$/)) {
score -= 10;
}
if (score > 100) {
score = 100;
}
if (score < 0) {
score = 0;
}
return score;
}
/**
* Checks for repetition of characters in
* a string
*
* @param {int} length Repetition length.
* @param {string} str The string to be checked.
* @return {string}
*/
function checkRepetition(length, str) {
var res = "", repeated = false;
for (var i = 0; i < str.length; i++) {
repeated = true;
for (var j = 0; j < length && (j + i + length) < str.length; j++) {
repeated = repeated && (str.charAt(j + i) === str.charAt(j + i + length));
}
if (j < length) {
repeated = false;
}
if (repeated) {
i += length - 1;
repeated = false;
}
else {
res += str.charAt(i);
}
}
return res;
}
/**
* Calculates background colors from percentage value.
*
* @param {int} perc The percentage strength of the password.
* @return {object} Object with colors as keys
*/
function calculateColorFromPercentage(perc) {
var minRed = 0;
var maxRed = 240;
var minGreen = 0;
var maxGreen = 240;
var blue = 10;
if (Object.prototype.hasOwnProperty.call(options.customColorBarRGB, 'red')) {
minRed = options.customColorBarRGB.red[0];
maxRed = options.customColorBarRGB.red[1];
}
if (Object.prototype.hasOwnProperty.call(options.customColorBarRGB, 'green')) {
minGreen = options.customColorBarRGB.green[0];
maxGreen = options.customColorBarRGB.green[1];
}
if (Object.prototype.hasOwnProperty.call(options.customColorBarRGB, 'blue')) {
blue = options.customColorBarRGB.blue;
}
var green = (perc * maxGreen / 50);
var red = (2 * maxRed) - (perc * maxRed / 50);
return {
red: Math.min(Math.max(red, minRed), maxRed),
green: Math.min(Math.max(green, minGreen), maxGreen),
blue: blue
}
}
/**
* Adds color styles to colorbar jQuery object.
*
* @param {jQuery} $colorbar The colorbar jquery object.
* @param {int} perc The percentage strength of the password.
* @return {jQuery}
*/
function addColorBarStyle($colorbar, perc) {
if (options.useColorBarImage) {
$colorbar.css({
backgroundPosition: "0px -" + perc + "px",
width: perc + '%'
});
}
else {
var colors = calculateColorFromPercentage(perc);
$colorbar.css({
'background-color': 'rgb(' + colors.red.toString() + ', ' + colors.green.toString() + ', ' + colors.blue.toString() + ')',
width: perc + '%'
});
}
return $colorbar;
}
/**
* Initializes the plugin creating and binding the
* required layers and events.
*
* @return {Password} Returns the Password instance.
*/
function init() {
var shown = true;
var $text = options.showText;
var $percentage = options.showPercent;
var $insert = $('<div>').addClass('progress mx-2').css({height: "3px"}).attr("role", "progressbar");
var $colorbar = $('<div>').addClass('progress-bar');
$insert.append($colorbar);
$object.closest(options.closestSelector).addClass('pass-strength-visible');
if (options.animate) {
$insert.css('display', 'none');
shown = false;
$object.closest(options.closestSelector).removeClass('pass-strength-visible');
}
if (options.showPercent) {
$percentage = $('<span>').addClass('pass-percent').text('0%');
$insert.append($percentage);
}
if (options.showText) {
$text = $('<span>').addClass('pass-text').html(options.enterPass);
$insert.append($text);
}
$object.closest(options.closestSelector).append($insert);
$object.keyup(function() {
var field = options.field || '';
if (field) {
field = $(field).val();
}
var score = calculateScore($object.val(), field);
$object.trigger('password.score', [score]);
var perc = score < 0 ? 0 : score;
$colorbar = addColorBarStyle($colorbar, perc);
if (options.showPercent) {
$percentage.html(perc + '%');
}
if (options.showText) {
var text = scoreText(score);
if (!$object.val().length && score <= 0) {
text = options.enterPass;
}
if ($text.html() !== $('<div>').html(text).html()) {
$text.html(text);
$object.trigger('password.text', [text, score]);
}
}
});
if (options.animate) {
$object.focus(function() {
if (!shown) {
$insert.slideDown(options.animateSpeed, function () {
shown = true;
$object.closest(options.closestSelector).addClass('pass-strength-visible');
});
}
});
$object.blur(function() {
if (!$object.val().length && shown) {
$insert.slideUp(options.animateSpeed, function () {
shown = false;
$object.closest(options.closestSelector).removeClass('pass-strength-visible')
});
}
});
}
return this;
}
return init.call(this);
};
$.fn.password = function(options) {
return this.each(function() {
new Password($(this), options);
});
};
})(jQuery);