Created
May 23, 2014 12:15
-
-
Save bitbonsai/e9c814d06d31ad1d8e41 to your computer and use it in GitHub Desktop.
Password Strenght PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function scorePassword($password){ | |
$score=0; | |
if(!$password) | |
return 0; | |
// award every unique letter until 5 repetitions | |
$letters=str_split($password); | |
$scores=array(); | |
foreach($letters as $letter){ | |
$scores[$letter]=(isset($scores[$letter]))?$scores[$letter]+1:1; | |
$score+=5/$scores[$letter]; | |
} | |
// bonus points for mixing it up | |
$variations = array( | |
preg_match('/\d/', $this->password), | |
preg_match('/[a-z]/', $this->password), | |
preg_match('/[A-Z]/', $this->password), | |
preg_match('/\W/', $this->password), | |
); | |
$variationCount=0; | |
foreach($variations as $check){ | |
$variationCount+=($check)?1:0; | |
} | |
$score+=($variationCount-1)*10; | |
return round($score); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment