PHP Compute Password Strength

Source: Internet
Author: User

The following PHP code is used to test the strength of a given password, with a maximum strength of 100

  1. /**
  2. *
  3. * @param String $string
  4. * @return Float
  5. *
  6. * Returns a float between 0 and 100. The Closer is the
  7. * The stronger password is; Further from 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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.