如何通过PHP评估密码的安全性?

更新于
2026-09-27 13:43:47
1阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计178个文字,预计阅读时间需要1分钟。

如何通过PHP评估密码的安全性?

以下是对给定内容的简化

php函数:测试密码强度参数:String $string返回:float返回值:0到100之间的浮点数。数值越接近100,密码强度越高。

phpfunction testPasswordStrength($string) { // 代码实现...}

如何通过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"));

本文共计178个文字,预计阅读时间需要1分钟。

如何通过PHP评估密码的安全性?

以下是对给定内容的简化

php函数:测试密码强度参数:String $string返回:float返回值:0到100之间的浮点数。数值越接近100,密码强度越高。

phpfunction testPasswordStrength($string) { // 代码实现...}

如何通过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"));