Php password strength calculation

Source: Internet
Author: User
Php password strength calculation

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
  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

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.