Something I was playing around with for a tool to roughly calculate the difficulty of guessing a password, just based on types of characters uses and the length of the password. (I.e., it does not try to consider things like dictionary words versus random strings, most commonly used characters, etc.)
/**
* Calculate the complexity of a password as the possible permutations
* for the types of characters used and the password length. Does not
* consider things like dictionary words, common patterns, et cetera
*
* @param string $password
* @return string (Numeric string from BCMath calculation)
*/
function PasswordComplexity($password)
{
if(strlen($password) < 1)
{
return '0';
}
$charsets = array(
'/[a-z]/' => 26,
'/[A-Z]/' => 26,
'/\d/' => 10,
'/[^a-zA-Z0-9]/' => 10, // kludge for any other characters!
);
$chars = 0;
foreach ($charsets as $regex => $num)
{
$chars += preg_match($regex, $password) ? $num : 0;
}
return bcpow($chars, strlen($password));
}