2015-05-03 147 views
0

我對密碼以下加密,當用戶註冊:的Yii登錄

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']; 
    // how many times the string will be hashed 
    $rounds = 10000; 
    // @todo Use real salt here. 
    $salt = 'salt'; 
    // pass in the password, the number of rounds, and the salt 
    // $5$ specifies SHA256-CRYPT, use $6$ if you really want SHA512 
    $model->PassWord=crypt($model->PassWord, sprintf('$6$rounds=%d$%s$',  $rounds, $salt)); 
    if($model->save()) 
     $this->redirect(array('view','id'=>$model->users_id)); 
    } 

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

現在我知道我需要改變,以驗證用戶身份的代碼是這樣的:

else if($user->PassWord!==$this->password) 

,如果我用的是crypt方法我通常會用這個:

else if($user->PassWord!==crypt($this->password,'salt')) 

如何更改爲L ogin腳本使用sha512

+0

那麼,什麼是您的實際問題/問題?顯示的代碼似乎不符合您的標題或描述。按照標題,你不能「解密」SHA-512,因爲它沒有加密,它是一個散列,因此是單向的。 –

+0

可能重複的[是否可以反轉sha1?](http://stackoverflow.com/questions/2235079/is-it-possible-to-reverse-a-sha1) –

+0

或者也許:[哈希差異密碼和加密](http://stackoverflow.com/questions/326699/difference-between-hashing-a-password-and-encrypting-it) –

回答

0

你應該在密碼哈希使用相同的參數從create

$rounds = 10000; 
$salt = 'salt'; 
if($user->PassWord !== crypt($this->password, sprintf('$6$rounds=%d$%s$', $rounds, $salt)))