WordPress.org

Make WordPress Core

Ticket #7124: patch.diff

File patch.diff, 3.3 KB (added by Otto42, 10 years ago)

Patch for password strength

  • C:/workspace/WP/wp-admin/js/password-strength-meter.js

     
    11// Password strength meter
    2 // This jQuery plugin is written by firas kassem [2007.04.05]
    3 // Firas Kassem  phiras.wordpress.com || phiras at gmail {dot} com
    4 // for more information : http://phiras.wordpress.com/2007/04/08/password-strength-meter-a-jquery-plugin/
    52
    63var shortPass = pwsL10n.short
    74var badPass = pwsL10n.bad
    85var goodPass = pwsL10n.good
    96var strongPass = pwsL10n.strong
    107
    11 
    128function passwordStrength(password,username) {
    13     score = 0
    14 
    15     //password < 4
     9        //password < 4
    1610    if (password.length < 4 ) { return shortPass }
    1711
    1812    //password == username
    1913    if (password.toLowerCase()==username.toLowerCase()) return badPass
    20 
    21     //password length
    22     score += password.length * 4
    23     score += ( checkRepetition(1,password).length - password.length ) * 1
    24     score += ( checkRepetition(2,password).length - password.length ) * 1
    25     score += ( checkRepetition(3,password).length - password.length ) * 1
    26     score += ( checkRepetition(4,password).length - password.length ) * 1
    27 
    28     //password has 3 numbers
    29     if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5
    30 
    31     //password has 2 sybols
    32     if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5
    33 
    34     //password has Upper and Lower chars
    35     if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10
    36 
    37     //password has number and chars
    38     if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15
    39     //
    40     //password has number and symbol
    41     if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15
    42 
    43     //password has char and symbol
    44     if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15
    45 
    46     //password is just a nubers or chars
    47     if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10
    48 
    49     //verifing 0 < score < 100
    50     if ( score < 0 )  score = 0
    51     if ( score > 100 )  score = 100
    52 
    53     if (score < 34 )  return badPass
    54     if (score < 68 )  return goodPass
     14   
     15        var symbolSize = 0;
     16        if (password.match(/[0-9]/)) symbolSize +=10;
     17        if (password.match(/[a-z]/)) symbolSize +=26;
     18        if (password.match(/[A-Z]/)) symbolSize +=26;
     19        if (password.match(/[^a-zA-Z0-9]/)) symbolSize +=31;
     20       
     21        var natLog = Math.log( Math.pow(symbolSize,password.length) );
     22        var score = natLog / Math.LN2;
     23    if (score < 40 )  return badPass
     24    if (score < 56 )  return goodPass
    5525    return strongPass
    56 }
    57 
    58 
    59 // checkRepetition(1,'aaaaaaabcbc')   = 'abcbc'
    60 // checkRepetition(2,'aaaaaaabcbc')   = 'aabc'
    61 // checkRepetition(2,'aaaaaaabcdbcd') = 'aabcd'
    62 
    63 function checkRepetition(pLen,str) {
    64     res = ""
    65     for ( i=0; i<str.length ; i++ ) {
    66         repeated=true
    67         for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
    68             repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
    69         if (j<pLen) repeated=false
    70         if (repeated) {
    71             i+=pLen-1
    72             repeated=false
    73         }
    74         else {
    75             res+=str.charAt(i)
    76         }
    77     }
    78     return res
    79 }
    80 
     26}
     27 No newline at end of file