function blowfishCrypt($password,$cost)
{
$chars='./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$salt=sprintf('$2y$%02d$',$cost);
//For PHP < PHP 5.3.7 use this instead
// $salt=sprintf('$2a$%02d$',$cost);
//Create a 22 character salt -edit- 2013.01.15 - replaced rand with mt_rand
mt_srand();
for($i=0;$i<22;$i++) $salt.=$chars[mt_rand(0,63)];
return crypt($password,$salt);
}
例:
$hash=blowfishCrypt('password',10); //This creates the hash
$hash=blowfishCrypt('password',12); //This creates a more secure hash
if(crypt('password',$hash)==$hash){ /*ok*/ } //This checks a password
bcrypt 使用Eksblowfish算法来散列密码 . 虽然Eksblowfish和Blowfish的加密阶段完全相同,但Eksblowfish的关键计划阶段确保任何后续状态都依赖于盐和密钥(用户密码),并且在不知道两者的情况下都不能预先计算任何状态 . Because of this key difference, bcrypt is a one-way hashing algorithm. 您无法在不知道盐的情况下检索纯文本密码,轮回 and key (密码) . [Source]
<?php
// See the password_hash() example to see where this came from.
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';
if (password_verify('rasmuslerdorf', $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
您可以使用 crypt() 函数生成输入字符串的bcrypt哈希值 . 此类可以自动生成salt并验证输入的现有哈希值 . If you are using a version of PHP higher or equal to 5.3.7, it is highly recommended you use the built-in function or the compat library . 此替代方案仅用于历史目的 .
class Bcrypt{
private $rounds;
public function __construct($rounds = 12) {
if (CRYPT_BLOWFISH != 1) {
throw new Exception("bcrypt not supported in this installation. See http://php.net/crypt");
}
$this->rounds = $rounds;
}
public function hash($input){
$hash = crypt($input, $this->getSalt());
if (strlen($hash) > 13)
return $hash;
return false;
}
public function verify($input, $existingHash){
$hash = crypt($input, $existingHash);
return $hash === $existingHash;
}
private function getSalt(){
$salt = sprintf('$2a$%02d$', $this->rounds);
$bytes = $this->getRandomBytes(16);
$salt .= $this->encodeBytes($bytes);
return $salt;
}
private $randomState;
private function getRandomBytes($count){
$bytes = '';
if (function_exists('openssl_random_pseudo_bytes') &&
(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) { // OpenSSL is slow on Windows
$bytes = openssl_random_pseudo_bytes($count);
}
if ($bytes === '' && is_readable('/dev/urandom') &&
($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) {
$bytes = fread($hRand, $count);
fclose($hRand);
}
if (strlen($bytes) < $count) {
$bytes = '';
if ($this->randomState === null) {
$this->randomState = microtime();
if (function_exists('getmypid')) {
$this->randomState .= getmypid();
}
}
for ($i = 0; $i < $count; $i += 16) {
$this->randomState = md5(microtime() . $this->randomState);
if (PHP_VERSION >= '5') {
$bytes .= md5($this->randomState, true);
} else {
$bytes .= pack('H*', md5($this->randomState));
}
}
$bytes = substr($bytes, 0, $count);
}
return $bytes;
}
private function encodeBytes($input){
// The following is code from the PHP Password Hashing Framework
$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$output = '';
$i = 0;
do {
$c1 = ord($input[$i++]);
$output .= $itoa64[$c1 >> 2];
$c1 = ($c1 & 0x03) << 4;
if ($i >= 16) {
$output .= $itoa64[$c1];
break;
}
$c2 = ord($input[$i++]);
$c1 |= $c2 >> 4;
$output .= $itoa64[$c1];
$c1 = ($c2 & 0x0f) << 2;
$c2 = ord($input[$i++]);
$c1 |= $c2 >> 6;
$output .= $itoa64[$c1];
$output .= $itoa64[$c2 & 0x3f];
} while (true);
return $output;
}
}
9 回答
PHP 5.5版将内置支持BCrypt,函数password_hash()和password_verify() . 实际上这些只是函数crypt()的包装器,并且应该更容易正确使用它 . 它负责生成安全的随机盐,并提供良好的默认值 .
使用此功能的最简单方法是:
此代码将使用BCrypt(算法
2y
)对密码进行哈希处理,从OS随机源生成随机盐,并使用默认成本参数(此时此值为10) . 如果用户输入的密码与已存储的哈希值匹配,则第二行检查 .如果要更改成本参数,可以这样做,将成本参数增加1,将计算哈希值所需的时间加倍:
与
"cost"
参数相比,最好省略"salt"
参数,因为该函数已经尽力创建加密安全盐 .对于PHP 5.3.7及更高版本,存在compatibility pack,来自同一作者的
password_hash()
函数 . 对于5.3.7之前的PHP版本,不支持带有2y
的crypt()
,即unicode安全的BCrypt算法 . 可以使用2a
替换它,这是早期PHP版本的最佳替代方案 .您将在Enough With The Rainbow Tables: What You Need To Know About Secure Password Schemes或Portable PHP password hashing framework中获得大量信息 .
目标是用一些缓慢的密码对密码进行哈希处理,所以有人获取你的密码数据库会死于试图强行它(检查密码的10毫秒延迟对你来说没什么,对于试图暴力破解它的人来说很多) . Bcrypt很慢,可以与参数一起使用来选择它的速度 .
那么,你想使用bcrypt吗? Awesome! 但是,像其他密码学领域一样,你不应该做错了 .
原因很简单:它很容易screw up bcrypt . 事实上,如果你仔细查看这个页面上的每一段代码,你至少会违反其中一个常见问题 .
面对它,密码学很难 .
留给专家吧 . 把它留给那些维护这些库的人 . 如果你需要做出决定,你做错了 .
相反,只需使用一个库 . 根据您的要求,有几种存在 .
图书馆
以下是一些更常见的API的细分 .
PHP 5.5 API - (适用于5.3.7)
从PHP 5.5开始,引入了用于散列密码的新API . 对于5.3.7,还有(由我)维护的填充程序兼容库 . 这有利于成为同行评审且易于使用的实现 .
真的,它的目标是非常简单 .
资源:
文档:on PHP.net
兼容性库:on GitHub
PHP的RFC:on wiki.php.net
Zend \ Crypt \ Password \ Bcrypt(5.3.2)
这是另一个类似于PHP 5.5的API,并且具有类似的用途 .
资源:
文档:on Zend
博客文章:Password Hashing With Zend Crypt
PasswordLib
这是一种稍微不同的密码散列方法 . PasswordLib不是简单地支持bcrypt,而是支持大量的哈希算法 . 它主要适用于需要支持与您可能无法控制的旧系统和不同系统兼容的环境 . 它支持大量的哈希算法 . 并支持5.3.2
参考文献:
PHPASS
这是一个支持bcrypt的层,但是也支持一个相当强大的算法,如果你不能访问PHP> = 5.3.2那么它很有用......它实际上支持PHP 3.0(虽然不支持bcrypt) .
资源
代码:cvsweb
项目地点:on OpenWall
对<5.3.0算法的评论:on StackOverflow
Note: 不要使用未在openwall上托管的PHPASS替代品,它们是不同的项目!
关于BCrypt
如果您注意到,这些库中的每一个都返回一个字符串 . 这是因为BCrypt在内部工作 . 关于这一点,有很多答案 . 这是我写的一个选择,我不会在这里复制/粘贴,但链接到:
Fundamental Difference Between Hashing And Encryption Algorithms - 解释有关它们的术语和一些基本信息 .
About reversing hashes without rainbow tables - 基本上我们应该在第一个中使用bcrypt地点...
Storing bcrypt Hashes - 基本上为什么salt和算法包含在哈希结果中 .
How to update the cost of bcrypt hashes - 基本上如何选择然后维护bcrypt哈希的成本 .
How to hash long passwords with bcrypt - 解释了bcrypt的72个字符的密码限制 .
How bcrypt uses salts
Best practices of salting and peppering passwords - 基本上,不要使用"pepper"
Migrating old md5 passwords to bcrypt
总结
有很多不同的选择 . 你选择哪个取决于你 . 但是,我建议你使用上面的一个库来为你处理这个问题 .
同样,如果你直接使用
crypt()
,你可能做错了什么 . 如果您的代码直接使用hash()
(或md5()
或sha1()
),那么您几乎肯定会做错事 .只需使用图书馆......
编辑:2013.01.15 - 如果您的服务器支持它,请改用martinstoeckli's solution .
每个人都想让它变得更复杂 . crypt()函数完成大部分工作 .
例:
我知道这应该是显而易见的,但请不要使用“密码”作为密码 .
bcrypt
是一种散列算法,可通过硬件进行扩展(通过可配置的轮数) . 它的缓慢和多轮确保攻击者必须部署大量资金和硬件才能破解您的密码 . 添加到每个密码salts(bcrypt
需要盐),你可以确定攻击几乎不可行,没有可笑的资金或硬件 .bcrypt
使用Eksblowfish算法来散列密码 . 虽然Eksblowfish和Blowfish的加密阶段完全相同,但Eksblowfish的关键计划阶段确保任何后续状态都依赖于盐和密钥(用户密码),并且在不知道两者的情况下都不能预先计算任何状态 . Because of this key difference, bcrypt is a one-way hashing algorithm. 您无法在不知道盐的情况下检索纯文本密码,轮回 and key (密码) . [Source]如何使用bcrypt:
使用PHP> = 5.5-DEV
密码散列函数have now been built directly into PHP >= 5.5 . 您现在可以使用password_hash()创建任何密码的
bcrypt
哈希:要针对现有哈希验证用户提供的密码,您可以使用password_verify():
使用PHP> = 5.3.7,<5.5-DEV(也是RedHat PHP> = 5.3.3)
基于最初用C编写的上述函数的源代码创建compatibility library GitHub,它提供相同的功能 . 安装兼容性库后,用法与上面相同(如果您仍在5.3.x分支上,则减去简写数组表示法) .
使用PHP <5.3.7(已弃用)
您可以使用
crypt()
函数生成输入字符串的bcrypt哈希值 . 此类可以自动生成salt并验证输入的现有哈希值 . If you are using a version of PHP higher or equal to 5.3.7, it is highly recommended you use the built-in function or the compat library . 此替代方案仅用于历史目的 .您可以像这样使用此代码:
或者,您也可以使用Portable PHP Hashing Framework .
你可以使用PHP的
crypt()
函数和bcrypt创建一个单向哈希,并传入适当的Blowfish盐 . 整个等式中最重要的是A)算法没有被破坏并且B) you properly salt each password . 不要使用全应用盐;这会打开整个应用程序,从一组Rainbow表中进行攻击 .PHP - Crypt Function
另一种方法是使用scrypt,专门设计为在his paper中由Colin Percival优于bcrypt . 有一个scrypt PHP extension in PECL . 理想情况下,这个算法将被转换为PHP,以便可以为password_ *函数指定(理想情况下为"PASSWORD_SCRYPT"),但那还没有 .
目前的想法:哈希应该是最慢的,而不是最快的 . 这抑制了rainbow table攻击 .
也相关,但预防:攻击者永远不应无限制地访问您的登录屏幕 . 为了防止这种情况:设置一个IP地址跟踪表,记录每次匹配以及URI . 如果在任何五分钟内从同一IP地址登录的次数超过5次,请进行解释 . 第二种方法是拥有一个双层密码方案,就像银行一样 . 在第二次通过时锁定故障会提高安全性 .
摘要:使用耗时的哈希函数减慢攻击者的速度 . 此外,阻止对您的登录进行过多访问,并添加第二个密码层 .
对于OAuth 2密码: