欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php计算密码强度

程序员文章站 2022-05-26 19:46:14
...

下面的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
相关标签: php计算密码强度