garybell / password-validator
通过密码熵值进行密码验证
2.2.1
2021-09-13 20:01 UTC
Requires
- php: ^7.4|^8.0|^8.1
Requires (Dev)
- phpunit/phpunit: ^9.4
README
使用熵值而不是任意规则来验证密码。
这是一个Lane Wagner的Go Password Validator的PHP版本。
该项目可用于作为密码强度计的前端,或在服务器上简单地验证密码强度。优势
- 没有愚蠢的规则(不要求大写字母、数字、特殊字符等)
- 一切基于熵(密码的原始加密强度)
- 受此XKCD启发
我应该使用多少熵值?
由您决定。但以下是一个很好的图表,显示了不同值的一些计时信息
大约在50-70范围内看起来是“平均”的
安装和使用
使用composer安装库
composer require garybell/password-validator
要使用功能,请使用密码作为参数调用类中的任何函数。所有函数都是静态的,因此在使用之前不需要创建PasswordValidator对象。
示例
// Get the base of the password (characters from different character sets used)
$base = GaryBell\PasswordValidator::getBase($password);
// get the length of the password (characters used (only allows 2 of any single character)
$length = GaryBell\PasswordValidator::getLength($password);
// get the entropy of the password
$entropy = GaryBell\PasswordValidator::getEntropy($password);
getEntropy
功能有一个可选参数decimalPlaces
,用于确定熵的精度。默认值为2位小数。要将精度降低到1位小数,使用
$entropy = GaryBell\PasswordValidator::getEntropy($password, 1);
类似地,对于4位小数,使用
$entropy = GaryBell\PasswordValidator::getEntropy($password, 4);
版本0.x和1.x
版本0.x和1.x不再受支持。它们仍然可以使用(因此对于正在运行PHP 7.3的用户)。1.x版本的最新版本是1.0.1。使用说明可在维基百科中找到
工作原理
首先,我们确定“基数”。基数是密码中找到的不同“字符集”的总和。
当前的字符集包括
- 26个小写字母
- 26个大写字母
- 10个数字
- 32个特殊字符 -
!"#$%&'()*+,-./:;<=>?@[\]^_{|}~
至少使用每个集中的一个字符,您的基数将是94:26+26+10+32 = 94
每个不匹配这些集的独特字符将向基数添加1
。
如果您只使用,例如,小写字母和数字,则您的基数将是36:26+10 = 36
。
在计算基数后,使用以下公式找到总暴力猜测数:base^length
使用基数26和7个字符的密码需要26^7
,或8031810176
次猜测。
一旦我们知道需要多少次猜测,我们可以使用ln(guesses)
计算实际的熵值(位)。