密码加密在Yii到sha512

时间:2015-05-03 17:24:27

标签: php encryption yii

我试图在Yii中对密码进行Sha512加密,但它没有工作

我相信代码是$model->PassWord=crypt($model->PassWord,'salt');

但我将此视为错误Fatal error: Call to undefined function CRYPT_SHA512() in protected/controllers/UsersController.php on line 73

有什么想法吗?

完整代码:

    public function actionCreate()
   {
       $model=new Users;

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['Users']))
{
    $model->attributes=$_POST['Users'];
    $model->PassWord=crypt_SHA512($model->PassWord,'salt');
    if($model->save())
        $this->redirect(array('view','id'=>$model->users_id));
}

$this->render('create',array(
    'model'=>$model,
));
   }

修改

这个$model->PassWord=crypt($model->PassWord,'$6$rounds=1212$16charactersaltt');使密码加密,太棒了!

现在尝试登录时出现此错误

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /web/users/m2052626/AST_14/Bugg.ly2/protected/components/UserIdentity.php on line 25

这是我最初用于登录的代码

public function authenticate()
    {
            $user=Users::model()->findByAttributes(array('email'=>$this->username));
    if($user===null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    //else if($record->PassWord!==md5($this->password))

    else if($user->PassWord!==($this->password))
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->_id=$user->users_id;
       // $this->setState('title', $record->title);
        $this->errorCode=self::ERROR_NONE;

我试图将此else if($user->PassWord!==($this->password))替换为此if (crypt($this->password '$6$rounds=1212$16charactersaltt') == $user->PassWord) { echo "password matched"; } else { echo "password did not match"; }无法正常工作

1 个答案:

答案 0 :(得分:1)

您的电话

$model->PassWord=crypt_SHA512($model->PassWord,'salt');

在我看来,你试图调用模块名称而不是函数。该函数仍然只是crypt(明文,盐);在salt字符串中,您选择哈希函数,并包括轮数(对于SHA加密)以及您的盐(在这种情况下应该正好为16个字符),以'$'分隔。所以你的电话会是这样的:

$model->PassWord=crypt($model->PassWord,'$6$rounds=1212$16charactersaltt');

在上面的代码中,salt字符串以$(必需)后跟6(SHA512哈希的编号)开头,然后$ round = 1212,其中1212是您希望SHA运行的轮数(必须是超过1000),最后是$和你的盐。

这是手册: http://php.net/crypt