| 1 | | function passwordStrength(password1, username, password2) { |
| | 1 | /** |
| | 2 | * Determine the strength of a given password |
| | 3 | * |
| | 4 | * @param string password1 The password |
| | 5 | * @param array blacklist An array of words that will lower the entropy of the password |
| | 6 | * @param string password2 The confirmed password |
| | 7 | */ |
| | 8 | function passwordStrength( password1, blacklist, password2 ) { |
| | 9 | if ( ! jQuery.isArray( blacklist ) ) |
| | 10 | blacklist = [ blacklist.toString() ]; |
| | 11 | |
| | 18 | |
| | 19 | /** |
| | 20 | * Builds an array of data that should be penalized, because it would lower the entropy of a password if it were used |
| | 21 | * |
| | 22 | * @return array The array of data to be blacklisted |
| | 23 | */ |
| | 24 | function buildUserInputBlacklist() { |
| | 25 | var i, userInputFieldsLength, rawValuesLength, currentField, |
| | 26 | rawValues = [], |
| | 27 | blacklist = [], |
| | 28 | userInputFields = [ 'user_login', 'first_name', 'last_name', 'nickname', 'display_name', 'email', 'url', 'description', 'weblog_title', 'admin_email' ]; |
| | 29 | |
| | 30 | // Collect all the strings we want to blacklist |
| | 31 | rawValues.push( jQuery( 'title' ).text() ); |
| | 32 | rawValues.push( document.URL ); |
| | 33 | |
| | 34 | userInputFieldsLength = userInputFields.length; |
| | 35 | for ( i = 0; i < userInputFieldsLength; i++ ) { |
| | 36 | currentField = jQuery( '#' + userInputFields[ i ] ); |
| | 37 | |
| | 38 | if ( 0 == currentField.length ) { |
| | 39 | continue; |
| | 40 | } |
| | 41 | |
| | 42 | rawValues.push( currentField[0].defaultValue ); |
| | 43 | rawValues.push( currentField.val() ); |
| | 44 | } |
| | 45 | |
| | 46 | // Strip out non-alphanumeric characters and convert each word to an individual entry |
| | 47 | rawValuesLength = rawValues.length; |
| | 48 | for ( i = 0; i < rawValuesLength; i++ ) { |
| | 49 | if ( rawValues[ i ] ) { |
| | 50 | blacklist = blacklist.concat( rawValues[ i ].replace( /\W/g, ' ' ).split( ' ' ) ); |
| | 51 | } |
| | 52 | } |
| | 53 | |
| | 54 | // Remove empty values, short words, and duplicates. Short words are likely to cause many false positives. |
| | 55 | blacklist = jQuery.grep( blacklist, function( value, key ) { |
| | 56 | if ( '' == value || 4 > value.length ) { |
| | 57 | return false; |
| | 58 | } |
| | 59 | |
| | 60 | return jQuery.inArray( value, blacklist ) === key; |
| | 61 | }); |
| | 62 | |
| | 63 | return blacklist; |
| | 64 | } |
| | 65 | No newline at end of file |