rych / phpass
PHP 密码库:PHP 中简单、安全的密码管理
v3.0.0beta1
2012-08-14 03:45 UTC
Requires
- php: >=5.3.8
- ext-hash: *
Suggests
- ext-mcrypt: The Mcrypt extension is used as a source of cryptographically strong random data in the absence of ext-openssl.
- ext-openssl: The OpenSSL extension is used as the default source of cryptographically strong random data.
This package is auto-updated.
Last update: 2024-09-23 11:20:58 UTC
README
PHP 密码库旨在简化与 PHP 中密码相关的任务。它能够生成强大的密码哈希,验证提供的密码字符串是否与这些哈希匹配,并使用各种算法计算密码字符串的强度。
本项目受以下项目的启发:Openwall 的 PHP 可移植哈希库 和 Python 的 PassLib。
功能
- 仅用几行代码即可创建和验证安全的密码哈希。
- 默认支持 bcrypt 和 PBKDF2。
- 易于扩展以支持其他哈希方法。
- 基于知名算法的额外密码强度组件。
- 遵循 PSR-0 标准,以实现自动加载器的兼容性。
安装
PEAR
通过 PEAR 安装很简单,只需包含 PEAR 频道 并安装 rych/PHPass
包。
pear channel-discover rchouinard.github.com/pear pear install rych/PHPass-2.1.0-alpha
Composer
Composer 是管理 PHP 项目的依赖项的一种简单方法。PHP 密码库可以在默认的 Packagist 仓库中找到。
在您的项目中安装 Composer 后,可以通过将以下行添加到您的 composer.json
文件并运行 Composer 命令行工具来安装 PHP 密码库
{ "require": { "rych/phpass": "2.1.0-dev" } }
用法
哈希密码
库提供了使用各种方法生成用户密码的强大密码哈希的能力。每种方法都可以根据需要进行自定义,并且在使用基类时还可以与 HMAC 哈希结合使用。
示例
使用默认的 bcrypt 适配器
<?php // Default configuration - bcrypt adapter, 2^12 (4,096) iterations $phpassHash = new \Phpass\Hash;
使用 PBKDF2 适配器
<?php // Customize hash adapter - PBKDF2 adapter, 15,000 iterations $adapter = new \Phpass\Hash\Adapter\Pbkdf2(array ( 'iterationCount' => 15000 )); $phpassHash = new \Phpass\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 \Phpass\Strength; // Returns 30 $passwordEntropy = $phpassStrength->calculate('MySecretPassword');
使用 Wolfram Alpha 算法 计算密码的熵
<?php // Custom strength adapter (Wolfram algorithm) $adapter = new \Phpass\Strength\Adapter\Wolfram; $phpassStrength = new \Phpass\Strength($adapter); // Returns 59 $passwordEntropy = $phpassStrength->calculate('MySecretPassword');