2012-05-17 18 views
0

我正在学习php安全在线(使用php 5.4),并遇到以下代码,我想了解/使用。下面的代码是否使用bcrypt并且它是blowfish的良好实现? 如果存在问题,请您提供修复或资源建议。谢谢。这段代码是否使用Bcrypt或者只是普通的河豚?

class PassHash { 

    // blowfish 
    private static $algo = '$2a'; 

    // cost parameter 
    private static $cost = '$10'; 

    // mainly for internal use 
    public static function unique_salt() { 
     return substr(sha1(mt_rand()),0,22); 
    } 

    // this will be used to generate a hash 
    public static function hash($password) { 

     return crypt($password, 
        self::$algo . 
        self::$cost . 
        '$' . self::unique_salt()); 

    } 

    // this will be used to compare a password against a hash 
    public static function check_password($hash, $password) { 

     $full_salt = substr($hash, 0, 29); 

     $new_hash = crypt($password, $full_salt); 

     return ($hash == $new_hash); 

    } 

} 

下面是用户注册时的用法:

// include the class 
require ("PassHash.php"); 
// ... 
// read all form input from $_POST 
// ... 
// do your regular form validation stuff 
// ... 
// hash the password 
$pass_hash = PassHash::hash($_POST['password']); 
// store all user info in the DB, excluding $_POST['password'] 
// store $pass_hash instead 
// ... 

这里是在用户登录过程中使用:

// include the class 
require ("PassHash.php");   
// read all form input from $_POST 
// ... 
// fetch the user record based on $_POST['username'] or similar 
// ... 
// ... 
// check the password the user tried to login with 
if (PassHash::check_password($user['pass_hash'], $_POST['password']) { 
    // grant access 
    // ... 
} else { 
    // deny access 
    // ... 
} 
+0

另外:我是否需要将盐存储在mysql中,还是将它作为passhash类的内部函数? –

+0

您不需要存储salt,它已经在PassHash :: hash的返回值中。是的,这是一种类似于bcrypt的技术。关于只用核心PHP就可以得到的一样好。 – Artefact2

+0

所以独特的盐包含在散列中?是不是像向黑客移交密钥或盐部分是否加密一样......盐和哈希应该不分开吗? –

回答

0

简短的回答:

是的ES使用bcrypt河豚(在PHP河豚是bcrypt当前算法)

正确答案:

为什么不使用受信任的PHP兼容库像this one?

使用这种过度的好处你发布的那个? :

  1. 广泛被许多人用来在这里(必须是可信的,深受社会各界采取)

  2. 允许使用PHP 5.5原生bcrypt功能向前兼容性(因此命名为passwd_compat)更多信息:Info Here!

  3. 允许改作它是天才(如果你决定要杀青算法的成本,你可以很容易地做到这一点,并检查费用相匹配的库文件,如果没有,那么你几乎可以只是更新密码)

底线:如果您不知道自己在做什么,那么只能用bcrypt出错。有一点要记住的是:如果已经有轮子出现,不要重新发明轮子。

希望这个答案可以帮助你/扩大你的知识。

相关问题