2011-05-27 122 views
0

我試圖存儲密碼加密格式,但它似乎並不奏效correcty。這裏是我使用的PHP代碼。PHP隱窩問題

function encryptMe($input, $salt){ 
    $output = crypt($input,$salt); 
return $output; 
} 

function getSalt(){ 
    //set number of repititions 
    $reps="5000"; 

    $salt = substr(str_replace('+', '.', base64_encode(
      pack('N4', mt_rand(), mt_rand(), mt_rand(), mt_rand()) 
      )), 0, 16); 
    $salt = "$6$"."rounds=".$reps."$".$salt;  
    return $salt;  
} 

我在我的代碼中也有以下聲明。

$input['password'] = $_POST['password']; 
$salt = getSalt(); 
$input['password'] = encryptMe($input['password'],$salt); 

我已經跑了這個多次用不同的鹽,但相同的密碼,並不斷得到相同的散列。改變鹽似乎沒有任何影響,我不知道什麼是錯的。有人可以看看這段代碼並幫助我嗎?

也就是有什麼辦法veryify,這是使用SHA512?

+0

如果你想始終使用SHA-512,看看[這裏](http://stackoverflow.com/questions/1966154/sha-512-library-for-php/1966162#1966162)代替使用'crypt()'。 – 2011-05-27 03:30:11

+0

您可以查看['CRYPT_SHA512'](http://php.net/manual/en/function.crypt.php)以查看SHA512是否可用。 – sarnold 2011-05-27 03:31:39

+0

我正在使用PHP 5.2,我無法找到使用散列函數的方法。 – user052211 2011-05-27 03:55:21

回答

1

這是因爲隱窩()返回很少的幾個字符,所以輸入,甚至是不同的,因爲只有最後一個字符改變仍可能返回相同的字符串。

另一種方法是使用hash()的SHA-256。有人在你的帖子中分享了一個非常有趣的鏈接。

編輯

這是怎麼vBulletin對密碼進行加密。不知道他們是否仍在使用這種方法。

$password_hash = md5(md5($password_text) . $user_salt); 
// $user_salt is a random three character string stored 
// in the user table as 'salt'. 
+0

我不認爲我可以使用散列函數鹽 – user052211 2011-05-27 04:05:19

+0

請參閱我的編輯上面:) – 2011-05-27 05:15:46