1 Jek.newPackage('Jek.algorithm');
   2 /**
   3  * Calculate the password complexity
   4  * 
   5  * @param {String}pwd
   6  *            password
   7  */
   8 Jek.algorithm.passwordComplexity = function(pwd) {
   9 	var score = 0;
  10 	var len = pwd.length;
  11 	var lg = '';
  12 	function log(s){
  13 		return lg += s + '\n';
  14 	};
  15 	function calcLenScore(n, inc){
  16 		if (len > n){ 
  17 			score += inc;
  18 			log('PASSWORD LENGTH: LEN='+ len + '>' + n+'; score+'+inc+' = '+score);
  19 		}
  20 	};
  21 	function calcRegScore(reg, inc, desc){
  22 		if(pwd.match(reg)){
  23 			score += inc;
  24 			log('PASSWORD REG: ' + desc + '; score+'+inc+' = '+score);
  25 			return true;
  26 		}
  27 		return false;
  28 	};
  29 	function calcBoolScore(b, inc, desc) {
  30 		if(b){
  31 			score += inc;
  32 			log('PASSWORD BOOL: ' + desc + '; score+'+inc+' = '+score);
  33 			return true;
  34 		}
  35 		return false;
  36 	}
  37 	function calcComplexity(scorePerc) {
  38 		var comp = '';
  39 		if (scorePerc < 20) comp = "VERY_WEAK";
  40 		if (scorePerc >= 20) comp = "WEAK";
  41 		if (scorePerc >= 50) comp = "GOOD";
  42 		if (scorePerc >= 70) comp = "STRONG";
  43 		if (scorePerc >= 90) comp = "VERY_STRONG";
  44 		return comp;
  45 	}
  46 	//*************************** PASSWORD LENGTH  *****************************
  47 	calcLenScore(4,4);
  48 	calcLenScore(6,4);
  49 	calcLenScore(8,4);
  50 	calcLenScore(10,4);
  51 	calcLenScore(15,4);
  52 	//*************************** LETTERS  *****************************
  53 	var az = calcRegScore(/[a-z]/,1,'LETTERS=a-z');
  54 	var AZ = calcRegScore(/[A-Z]/,4,'LETTERS=A-Z');
  55 	//*************************** NUMBERS *****************************
  56 	var numb = calcRegScore(/\d+/,4,'NUMBERS>=1');
  57 	calcRegScore(/(.*[0-9].*[0-9].*[0-9])/,5,'NUMBERS>=3');
  58 	//*************************** SPECIAL CHAR *****************************
  59 	var special = calcRegScore(/.[!,@,#,$,%,^,&,*,?,_,~]/,6,'SPECIAL CHARS>=1');
  60 	calcRegScore(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/,6,'SPECIAL CHARS>=2');
  61 	//*************************** COMBOS *****************************
  62 	calcBoolScore((az && AZ),3,'LETTERS=a-z & A-Z');
  63 	calcBoolScore(((az || AZ) && numb),3, 'LETTERS & NUMBERS');
  64 	calcBoolScore((az || AZ || numb) && special, 8, 'LETTERS|NUMBERS & SPECIAL CHARS');
  65 	
  66 	score = Jek.roundTo((score/60)*100,0);
  67 	log('SCORE PERCENTAGE: ' + score + '%');
  68 	return {
  69 		score : score,
  70 		complexity : calcComplexity(score),
  71 		log : lg
  72 	};
  73 };