php計算密碼強度

fn2n4 9年前發布 | 1K 次閱讀 PHP

下面的php代碼用于測試給定密碼的強度,最高強度為100

<?php
/**

  • @param String $string
  • @return float
  • Returns a float between 0 and 100. The closer the number is to 100 the
  • the stronger password is; further from 100 the weaker the password is. */ function password_strength($string){ $h = 0; $size = strlen($string); foreach(count_chars($string, 1) as $v){
     $p = $v / $size;
     $h -= $p * log($p) / log(2);
    
    } $strength = ($h / 4) * 100; if($strength > 100){
     $strength = 100;
    
    } return $strength; }

var_dump(password_strength("Correct Horse Battery Staple")); echo "<br>"; var_dump(password_strength("Super Monkey Ball")); echo "<br>"; var_dump(password_strength("Tr0ub4dor&3")); echo "<br>"; var_dump(password_strength("abc123")); echo "<br>"; var_dump(password_strength("sweet"));</pre>

 本文由用戶 fn2n4 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!