当前位置:Gxlcms > PHP教程 > php计算密码强度

php计算密码强度

时间:2021-07-01 10:21:17 帮助过:27人阅读

下面的php代码用于测试给定密码的强度,最高强度为100

  1. /**
  2. *
  3. * @param String $string
  4. * @return float
  5. *
  6. * Returns a float between 0 and 100. The closer the number is to 100 the
  7. * the stronger password is; further from 100 the weaker the password is.
  8. */
  9. function password_strength($string){
  10. $h = 0;
  11. $size = strlen($string);
  12. foreach(count_chars($string, 1) as $v){
  13. $p = $v / $size;
  14. $h -= $p * log($p) / log(2);
  15. }
  16. $strength = ($h / 4) * 100;
  17. if($strength > 100){
  18. $strength = 100;
  19. }
  20. return $strength;
  21. }
  22. var_dump(password_strength("Correct Horse Battery Staple"));
  23. echo "
    ";
  24. var_dump(password_strength("Super Monkey Ball"));
  25. echo "
    ";
  26. var_dump(password_strength("Tr0ub4dor&3"));
  27. echo "
    ";
  28. var_dump(password_strength("abc123"));
  29. echo "
    ";
  30. var_dump(password_strength("sweet"));

php

人气教程排行