2012-10-15 50 views
0

我已經使用thisthis的密碼哈希腳本,我得到它的除了一些時間地穴功能給哈希爲「* 0」正常工作,並那麼它就會失敗。crypt()函數沒有給予適當的哈希

PHP代碼

$password='password'; 
    $salt = '$2y$07$'; 
    $salt .= base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_RANDOM)); 
    $salt .='$$'; 
    $password_hash = crypt($password, $salt)'; 

    echo $password_hash.'<br />'; 

使用上面我得到的值作爲

$salt = '$2y$07$8K3i8rJ7n7bsJA36CfbabQ==$$'; 
    $crypt_password = $password_hash; 
    $crypt_password = '$2y$07$8K3i8rJ7n7bsJA36CfbabO9ojj2hl61azl8CubJQhRTgla4ICiCVC'; 
    if (crypt($password,$crypt_password)===$crypt_password) 
    { 
    echo 'password verified'; 
    } 
    else{ 
    echo 'password NOT verified'; 
    } 

請參閱並提出任何可能的方式,使其正常工作。

謝謝。

+0

'$ crypt_password = $ password_hash'對不起,我沒有提到上述 –

+0

肯定的,因爲鹽必須是隨機的。 –

+1

驗證步驟是正確的('crypt'從'crypt'返回的「散列」中提取鹽)。問題是,'base64_encode'會發出無效字符,例如'+'(接受字符''crypt':'「./0-9A-Za-z」')。如果'base64_encode'返回一個無效字符'「* 0」'返回。 – vstm

回答

4

問題是base64_encode可能會生成一個帶有'+'符號的字符串,它被crypt函數視爲不正確的鹽。

var_dump$salt$password一起,你會看到每個+字符在鹽使用的時候,墓穴函數會返回一個字符串'*0' - 失敗的標誌。

解決它與替換所有的「+」號的一個可能的方法「」:

$salt = str_replace('+', '.', $salt); 
+0

我應該在base64_encode上使用一些字符串替換。 –

+0

謝謝,是的,我明白了。 –