| 87 | | var shortPass = 'Too short' |
| 88 | | var badPass = 'Bad' |
| 89 | | var goodPass = 'Good' |
| 90 | | var strongPass = 'Strong' |
| 91 | | |
| 92 | | |
| 93 | | |
| 94 | | function passwordStrength(password,username) |
| 95 | | { |
| 96 | | score = 0 |
| 97 | | |
| 98 | | //password < 4 |
| 99 | | if (password.length < 4 ) { return shortPass } |
| 100 | | |
| 101 | | //password == username |
| 102 | | if (password.toLowerCase()==username.toLowerCase()) return badPass |
| 103 | | |
| 104 | | //password length |
| 105 | | score += password.length * 4 |
| 106 | | score += ( checkRepetition(1,password).length - password.length ) * 1 |
| 107 | | score += ( checkRepetition(2,password).length - password.length ) * 1 |
| 108 | | score += ( checkRepetition(3,password).length - password.length ) * 1 |
| 109 | | score += ( checkRepetition(4,password).length - password.length ) * 1 |
| 110 | | |
| 111 | | //password has 3 numbers |
| 112 | | if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) score += 5 |
| 113 | | |
| 114 | | //password has 2 sybols |
| 115 | | if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 |
| 116 | | |
| 117 | | //password has Upper and Lower chars |
| 118 | | if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) score += 10 |
| 119 | | |
| 120 | | //password has number and chars |
| 121 | | if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) score += 15 |
| 122 | | // |
| 123 | | //password has number and symbol |
| 124 | | if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)) score += 15 |
| 125 | | |
| 126 | | //password has char and symbol |
| 127 | | if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)) score += 15 |
| 128 | | |
| 129 | | //password is just a nubers or chars |
| 130 | | if (password.match(/^\w+$/) || password.match(/^\d+$/) ) score -= 10 |
| 131 | | |
| 132 | | //verifing 0 < score < 100 |
| 133 | | if ( score < 0 ) score = 0 |
| 134 | | if ( score > 100 ) score = 100 |
| 135 | | |
| 136 | | if (score < 34 ) return badPass |
| 137 | | if (score < 68 ) return goodPass |
| 138 | | return strongPass |
| 139 | | } |
| 140 | | |
| 141 | | |
| 142 | | // checkRepetition(1,'aaaaaaabcbc') = 'abcbc' |
| 143 | | // checkRepetition(2,'aaaaaaabcbc') = 'aabc' |
| 144 | | // checkRepetition(2,'aaaaaaabcdbcd') = 'aabcd' |
| 145 | | |
| 146 | | function checkRepetition(pLen,str) { |
| 147 | | res = "" |
| 148 | | for ( i=0; i<str.length ; i++ ) { |
| 149 | | repeated=true |
| 150 | | for (j=0;j < pLen && (j+i+pLen) < str.length;j++) |
| 151 | | repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen)) |
| 152 | | if (j<pLen) repeated=false |
| 153 | | if (repeated) { |
| 154 | | i+=pLen-1 |
| 155 | | repeated=false |
| 156 | | } |
| 157 | | else { |
| 158 | | res+=str.charAt(i) |
| 159 | | } |
| 160 | | } |
| 161 | | return res |
| 162 | | } |