2014-09-30 156 views
6

其前面已經討論過,但似乎沒有任何結論。將密碼哈希從md5升級到bcrypt

理想的情況下,不希望維護狀態(升級/不升級)的數據庫等,所以,這裏是我在想什麼:

bcrypt的MD5'd密碼,並使用「用戶名+別的東西「作爲鹽。

  1. 這個方案有什麼意義嗎?
  2. 此外,通常使用用戶名作爲鹽的一部分是一個好主意?我在某處讀到,爲每個散列添加不同的鹽使其更安全。這是否正確(特別是在bcrypt的情況下)?
+0

請解釋downvote – merlinbeard 2014-09-30 03:57:28

+4

這會更適合[security.se],但請在發佈之前查看他們的幫助中心。 – 2014-09-30 03:57:48

+0

查看'password_hash()'http://php.net/manual/en/function.password-hash.php – timgavin 2014-09-30 03:59:20

回答

6

當然,切換到更安全的散列算法是一個好主意。還有一個功能password_hash()可用於創建BCrypt哈希:

// Hash a new password for storing in the database. 
// The function automatically generates a cryptographically safe salt. 
$hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT); 

// Check if the hash of the entered login password, matches the stored hash. 
// The salt and the cost factor will be extracted from $existingHashFromDb. 
$isPasswordCorrect = password_verify($password, $existingHashFromDb); 

從你的答案我猜你使用的無鹽MD5值,所以雙散列可以在這裏得到很好的解決。只需將MD5散列傳遞給password_hash()函數,它將自行生成一個安全的salt。

// Migrating the old MD5 hashes to MD5-BCrypt 
$hashToStoreInDb = password_hash($existingMd5Hash, PASSWORD_DEFAULT); 

驗證第一個check for a double hash,然後驗證相應的密碼。

if (checkIfDoubleHash($existingHashFromDb)) 
{ 
    $isPasswordCorrect = password_verify(MD5($password), $existingHashFromDb); 

    // Update database with pure BCrypt hash 
    if ($isPasswordCorrect) 
    $hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT); 
} 
else 
{ 
    $isPasswordCorrect = password_verify($password, $existingHashFromDb) 
} 

存儲的哈希值可以由領導$或單獨的數據庫字段來識別,例如BCrypt哈希總是以$字符開頭,MD5哈希沒有。

鹽應該而不是被從其他參數詮釋,它應該是唯一的每個密碼。 password_hash()函數將處理這個問題。由於必須在每個鹽之前建立彩虹色表,攻擊者必須爲每個密碼創建一個彩虹表。欲瞭解更多信息,你可以看看我的教程secure password storing

+0

這很有用。謝謝! – merlinbeard 2014-09-30 09:04:13