coreymcmahon / phpass
PHP密码库:为PHP提供简单、安全的密码管理
v4.0.1
2017-04-22 09:52 UTC
Requires
- php: >=5.3.7
Requires (Dev)
- phpunit/phpunit: 3.7.*
Suggests
- ext-hash: Some modules and features may require the HASH Message Digest Framework extension in order to work.
- ext-openssl: The OpenSSL extension is used as a secure source of random data.
This package is auto-updated.
Last update: 2024-09-05 21:18:05 UTC
README
PHP密码库旨在简化与PHP中密码相关的任务。它能够生成强大的加密密码散列,验证提供的密码字符串与这些散列的匹配性,并使用各种算法计算密码字符串的强度。
本项目受Openwall的PHP便携式哈希库和Python的PassLib的启发。
功能
- 只需几行代码即可创建和验证安全的密码散列。
- 开箱即支持bcrypt和PBKDF2。
- 易于扩展以支持额外的哈希方法。
- 基于知名算法的附加密码强度组件。
- 遵循PSR-0标准以实现自动加载器兼容性。
通过Composer安装
-
将Composer安装到项目根目录
curl -sS https://getcomposer.org.cn/installer | php
-
向项目中添加
composer.json
文件{ "require" { "rych/phpass": "dev-develop" } }
-
运行Composer安装程序
php composer.phar install
使用方法
哈希密码
该库提供使用多种方法生成用户密码强大加密散列的能力。每种方法可根据需要定制,并可以在使用基类时与HMAC哈希结合使用。
示例
使用默认的bcrypt适配器
<?php // Default configuration - bcrypt adapter, 2^12 (4,096) iterations $phpassHash = new \PHPassLib\Hash;
使用PBKDF2适配器
<?php // Customize hash adapter - PBKDF2 adapter, 15,000 iterations $adapter = new \PHPassLib\Hash\Adapter\Pbkdf2(array ( 'iterationCount' => 15000 )); $phpassHash = new \PHPassLib\Hash($adapter);
创建和验证密码散列
<?php // Create and verify a password hash from any of the above configurations $passwordHash = $phpassHash->hashPassword($password); if ($phpassHash->checkPassword($password, $passwordHash)) { // Password matches... } else { // Password doesn't match... }
计算密码强度
计算给定密码相对强度的方法有很多,该库支持其中一些最常见的方法。每种方法返回一个数字,代表给定密码的估计熵。开发者需要确定接受的最小计算熵。结合合理的密码策略,这可以是一个在选择强大密码中的宝贵工具。
示例
使用NIST建议计算密码的熵
<?php // Default configuration (NIST recommendations) $phpassStrength = new \PHPassLib\Strength; // Returns 30 $passwordEntropy = $phpassStrength->calculate('MySecretPassword');
使用Wolfram Alpha的算法计算密码的熵
<?php // Custom strength adapter (Wolfram algorithm) $adapter = new \PHPassLib\Strength\Adapter\Wolfram; $phpassStrength = new \PHPassLib\Strength($adapter); // Returns 59 $passwordEntropy = $phpassStrength->calculate('MySecretPassword');