php calculate password strength

WBOY
Release: 2016-07-25 08:44:13
Original
965 people have browsed it

The following php code is used to test the strength of a given password, the maximum strength is 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"));
Copy code

php


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template