Advertisement
Quimbox

Seguridad de contraseña PHP

May 22nd, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This function calculates how strong a password choice is. A strong  
  4.  * password has a value that is as close to possible to 100.
  5. */
  6. /**
  7.  *  
  8.  * @param String $string
  9.  * @return float
  10.  *  
  11.  * Returns a float between 0 and 100. The closer the number is to 100 the
  12.  * the stronger password is; further from 100 the weaker the password is.
  13.  */
  14. function password_strength($string){
  15.     $h    = 0;
  16.     $size = strlen($string);
  17.     foreach(count_chars($string, 1) as $v){
  18.         $p = $v / $size;
  19.         $h -= $p * log($p) / log(2);
  20.     }
  21.     $strength = ($h / 4) * 100;
  22.     if($strength > 100){
  23.         $strength = 100;
  24.     }
  25.     return $strength;
  26. }
  27.  
  28. var_dump(password_strength("Correct Horse Battery Staple"));
  29. echo "<br>";
  30. var_dump(password_strength("Super Monkey Ball"));
  31. echo "<br>";
  32. var_dump(password_strength("Tr0ub4dor&3"));
  33. echo "<br>";
  34. var_dump(password_strength("abc123"));
  35. echo "<br>";
  36. var_dump(password_strength("sweet"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement