phpnode/yiipassword

Yii PHP 框架的密码策略。

安装次数: 174,861

依赖者: 3

建议者: 0

安全: 0

星标: 75

关注者: 13

分支: 28

开放问题: 2

类型:yii-extension

1.0.3 2014-06-04 11:26 UTC

This package is not auto-updated.

Last update: 2024-09-11 11:58:59 UTC


README

密码策略是关于密码编码和验证方式的规范,以及用户提供的密码应该有多复杂。默认情况下,它包含bcrypt策略和多轮散列函数(如sha1)的策略,以及支持旧密码散列(如未加盐的md5和未加盐的sha1)。目标是允许多种不同的密码策略共存,并在用户登录时将用户从旧散列升级到新散列。

安装

安装composer(遵循https://getcomposer.org.cn/上的说明)然后运行

composer require phpnode/yiipassword

我为什么要用这个?

想象一下,你有一个使用简单、未加盐的md5基于密码散列的旧应用程序,这在2012年被认为完全不安全。你想升级你的密码散列,但你无法访问明文密码。在这种情况下,你可以配置两个密码策略,你的旧遗产一个使用md5,你的新闪亮一个使用bcrypt。然后当用户登录到他们的账户时,他们的密码将使用旧策略进行验证,如果匹配,他们将被无缝升级到新的bcrypt密码策略。例如

class User extends CActiveRecord
{
	public function behaviors()
	{
		return array(
			"PasswordBehavior" => array(
				"class" => "YiiPassword\Behavior",
				"defaultStrategyName" => "bcrypt",
				"strategies" => array(
					"bcrypt" => array(
						"class" => "YiiPassword\Strategies\Bcrypt",
						"workFactor" => 14
					),
					"legacy" => array(
						"class" => "YiiPassword\Strategies\LegacyMd5",
					)
				),
			)
		);
	}

	....
}

$user = User::model()->findByPK(1); // a user using the legacy password strategy
echo $user->password; // unsalted md5, horrible
$user->verifyPassword("password"); // verifies the password using the legacy strategy, and rehashes based on bcrypt strategy
echo $user->password; // now hashed with bcrpt

但这对于现代应用程序也有用,比如说你有一个新的webapp,你正在做正确的事情,使用bcrypt进行密码散列。你最初的工作因子是12,但几个月后,你决定想将其增加到15。通常这很困难实现,因为已经有很多用户使用了不安全的散列,但有了密码策略,你可以简单地添加另一个具有所需工作因子的bcrypt策略,将其设置为默认值,然后用户下次登录时将被升级到新的策略。

默认情况下,YiiPassword\Behavior假设你的模型包含以下字段

* *salt* - holds the per user salt used for hashing passwords
* *username* - holds the username
* *password* - holds the hashed password
* *passwordStrategy* - holds the name of the current password strategy for this user
* *requiresNewPassword* - a boolean field that determines whether the user should change their password or not

你可以配置行为中的字段名称。

另外信息:使用Bcrypt Strategy For New Application? - #10