集成password_compat库

时间:2013-08-13 23:03:38

标签: php codeigniter bcrypt

这不是我第一次使用CodeIgniter或Bcrypt,但这是我第一次使用specific library of Bcrypt with CodeIgniter.我遇到了将两者整合在一起的问题。

让我们来看看代码:

 public function create_user($value) {
        $this -> CI = get_instance(); // Get the CI instance
        $this -> CI -> load -> library('bcrypt'); // Use this to load the library from within the model 

        $hash = $this -> CI -> bcrypt -> password_hash($value[":password"], PASSWORD_BCRYPT, array("cost" => 17)); Here is where things get shaky.

        $value[":password"] = $hash; // Here I take the :password placeholder and update the clear text password with the bcrypt pasword

        var_dump($value[":password"]); // This gives me NULL, I assume because I am getting errors with $hash = .......

 ........................................

根据Password_compat手册:

  

BCRYPT还允许您在选项中定义成本参数   阵列。这允许您更改算法的CPU成本:

     

$ hash = password_hash($ password,PASSWORD_BCRYPT,array(“cost”=>   10));

在我的前端,这些是我一直在犯的错误:

  

消息:使用未定义的常量PASSWORD_BCRYPT - 假设   'PASSWORD_BCRYPT'

     

消息:password_hash()期望参数2为long,给定字符串

我把它变成了一个库本身,所以我将它放入application / librarys文件夹

This is the file

任何帮助都会很棒。谢谢。

1 个答案:

答案 0 :(得分:0)

好的well looking at this file(这是我原来的Bcrypt.php文件)

您注意到我没有添加这些行:

if (!defined('PASSWORD_DEFAULT')) {

    define('PASSWORD_BCRYPT', 1);
    define('PASSWORD_DEFAULT', PASSWORD_BCRYPT);

我删除它们的原因是因为我把上面的代码片段放在了Bcrpyt {...行下面会导致错误。

现在我把这段代码:

if (!defined('PASSWORD_DEFAULT')) {
        define('PASSWORD_BCRYPT', 1);
        define('PASSWORD_DEFAULT', PASSWORD_BCRYPT);

  Class Bcrypt {

这是我必须做的所有工作。傻我!现在它起作用了:

  

的字符串(60)   “$ $ 2Y $ 17 9qgFDbN3361DAQFilGZySuJ4czachQThuskoSj4DihkxjwGFqTx2e”