Created
October 21, 2013 07:03
-
-
Save weishuaiwang/7079714 to your computer and use it in GitHub Desktop.
password strength
This file contains 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
<?php | |
/** | |
* | |
* @param String $string | |
* @return float | |
* | |
* Returns a float between 0 and 100. The closer the number is to 100 the | |
* the stronger password is; further from 100 the weaker the password is. | |
*/ | |
function password_strength($string){ | |
$h = 0; | |
$size = strlen($string); | |
foreach(count_chars($string, 1) as $v){ | |
$p = $v / $size; | |
$h -= $p * log($p) / log(2); | |
} | |
$strength = ($h / 4) * 100; | |
if($strength > 100){ | |
$strength = 100; | |
} | |
return $strength; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var_dump(password_strength("Correct Horse Battery Staple"));
echo "
";
var_dump(password_strength("Super Monkey Ball"));
echo "
";
var_dump(password_strength("Tr0ub4dor&3"));
echo "
";
var_dump(password_strength("abc123"));
echo "
";
var_dump(password_strength("sweet"));