Skip to content

Instantly share code, notes, and snippets.

@JDMcKinstry
Created May 6, 2016 21:32
Show Gist options
  • Save JDMcKinstry/81c25be39c5c01d72ef56363ff1f5efa to your computer and use it in GitHub Desktop.
Save JDMcKinstry/81c25be39c5c01d72ef56363ff1f5efa to your computer and use it in GitHub Desktop.
function getPasswordStrength(pass) { /* 100% = r4R#$3fR5$h&K6n@yU^ */
var v = pass + '';
var regSymbols = new RegExp(/`|~|!|@|#|\$|%|\^|&|\*|\(|\)|_|-|\+|=|{|}|\[|]|\\|\||:|;|"|'|<|>|,|\.|\?|\//g),
regConsecutiveSymbols = new RegExp(/(`|~|!|@|#|\$|%|\^|&|\*|\(|\)|_|-|\+|=|{|}|\[|]|\\|\||:|;|"|'|<|>|,|\.|\?|\/){2,}/g);
var z = {
size: v.length
, numbers: v.match(/[0-9]/g)
, caseLower: v.match(/[a-z]/g)
, caseUpper: v.match(/[A-Z]/g)
, symbols: v.match(regSymbols)
, consecutiveNumbers: v.match(/[0-9]{2,}/g)
, consecutiveLetters: v.match(/[a-z]{3,}|[A-Z]{3,}/g)
, consecutiveLettersMixed: v.match(/[a-zA-Z]{3,}/g)
//, consecutiveLettersLower: v.match(/[a-z]{2,}/g)
//, consecutiveLettersUpper: v.match(/[A-Z]{2,}/g)
, consecutiveSymbols: v.match(regConsecutiveSymbols)
}
if (z.size > 7) {
var w = {
size: z.size > 17 ? 20 : ((z.size - 7) * 2)
, numbers: z.numbers ? (z.numbers.length > 4 ? 20 : (z.numbers.length * 5)) : 0
, caseLower: z.caseLower ? (z.caseLower.length > 4 ? 20 : (z.caseLower.length * 5)) : 0
, caseUpper: z.caseUpper ? (z.caseUpper.length > 4 ? 20 : (z.caseUpper.length * 5)) : 0
, symbols: z.symbols ? (z.symbols.length >= 3 ? 20 : (z.symbols.length * 6)) : 0
},
difference = 0;
// check for consecutive numbers
if (z.consecutiveNumbers) {
var doubles = 0,
triples = 0,
excess = 0,
total = 0;
for (var x in z.consecutiveNumbers) {
var cn = z.consecutiveNumbers[x] + '';
if (!cn.split(cn.charAt(0)).filter(function(el) {return el.length != 0}).length) {
switch (cn.length) {
case 2:
doubles -= 3;
if (doubles < -25) doubles = -25;
break;
case 3:
triples -= 3;
if (triples < -25) triples = -25;
break;
default:
excess = -50;
}
}
else if (cn == cn.split('').sort(function(a,b){return a-b;}).join('') || cn == cn.split('').sort(function(a,b){return b-a;}).join('')) {
switch (cn.length) {
case 2:
doubles -= 2;
if (doubles < -20) doubles = -20;
break;
case 3:
triples -= 2;
if (triples < -20) triples = -20;
break;
default:
excess = ~~((excess*2) - 2);
if (excess < -20) excess = -20;
}
}
}
total = doubles + triples + excess;
if (total < -50) total = -50;
difference += total;
}
// check for consecutive letters of Case matching
if (z.consecutiveLetters) {
var triples = 0,
quads = 0,
excess = 0,
total = 0;
for (var x in z.consecutiveLetters) {
var cl = z.consecutiveLetters[x];
if (!cl.split(cl.charAt(0)).filter(function(el) {return el.length != 0}).length) {
switch (cl.length) {
case 3:
triples -= 3;
if (triples < -25) triples = -25;
break;
case 4:
quads -= 2;
if (quads < -20) quads = -20;
break;
default:
excess = -50;
}
}
else if (cl == cl.split('').sort(function(a,b){return a>b?1:a<b?-1:0;}).join('') || cl == cl.split('').sort(function(a,b){return a>b?-1:a<b?1:0;}).join('')) {
switch (cl.length) {
case 3:
triples -= 2;
if (triples < -20) triples = -20;
break;
case 4:
quads -= 2;
if (quads < -20) quads = -20;
break;
default:
excess = ~~((excess*2) - 2);
if (excess < -20) excess = -20;
}
}
}
total = triples + quads + excess;
if (total < -50) total = -50;
difference += total;
}
// check for consecutive letters of any case
if (z.consecutiveLettersMixed) {
var triples = 0,
quads = 0,
excess = 0,
total = 0;
for (var x in z.consecutiveLettersMixed) {
var clm = z.consecutiveLettersMixed[x];
if (!clm.split(clm.charAt(0)).filter(function(el) {return el.length != 0}).length) {
switch (clm.length) {
case 3:
triples -= 2;
if (triples < -20) triples = -20;
break;
case 4:
quads -= 2;
if (quads < -20) quads = -20;
break;
default:
excess = ~~((excess*2) - 2);
if (excess < -20) excess = -20;
}
}
else if (clm == clm.split('').sort(function(a,b){return a>b?1:a<b?-1:0;}).join('') || clm == clm.split('').sort(function(a,b){return a>b?-1:a<b?1:0;}).join('')) {
switch (clm.length) {
case 3:
triples -= 1;
if (triples < -10) triples = -10;
break;
case 4:
quads -= 1;
if (quads < -10) quads = -10;
break;
default:
excess = ~~((excess*2) - 1);
if (excess < -15) excess = -15;
}
}
}
total = triples + quads + excess;
if (total < -50) total = -50;
difference += total;
}
var grandTotal = 0;
for (var x in w) if (w[x]) grandTotal += w[x];
grandTotal += difference
return grandTotal > 100 ? 100 : grandTotal;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment