2016-05-21 23 views
3

所以這個問題在標題^^中很多。PHP password_hash()+ password_verify()今天安全嗎(2016年5月)?

下面是一個小小的php代碼,我測試了我的服務器性能(+結果的截圖),並向您展示了我打算如何使用password_hash()和password_verify()。

我想我會用PASSWORD_BCRYPT和成本= 11,你覺得怎麼樣?

<?php 
$startpage = microtime(true); 
$userPassword = "ILike5InchesIceCubes"; 
echo "<h2>Password we work on : " . $userPassword . "</h2></br></br>"; 


echo "<b>password_hash($userPassword, PASSWORD_BCRYPT) :</br></b>"; 
$start1 = microtime(true); 
$hash = password_hash($userPassword, PASSWORD_BCRYPT); 
echo "Hash is : " . $hash . "</br>"; 
echo "Encryption took : ". (microtime(true) - $start1) . " seconds </br>"; 
$start2 = microtime(true); 
password_verify($userPassword, $hash); 
echo "Password verification took : ". (microtime(true) - $start2) ." seconds </br></br>"; 

echo "<b>password_hash($userPassword, PASSWORD_DEFAULT) :</br></b>"; 
$start1 = microtime(true); 
$hash = password_hash($userPassword, PASSWORD_DEFAULT); 
echo "Hash is : " . $hash . "</br>"; 
echo "Encryption took : ". (microtime(true) - $start1) . " seconds </br>"; 
$start2 = microtime(true); 
password_verify($userPassword, $hash); 
echo "Password verification took : ". (microtime(true) - $start2) ." seconds </br></br>"; 

$cost = 4; 
do { 

     echo "<b>password_hash($userPassword, PASSWORD_BCRYPT, [\"cost\" =>" . $cost . "])</br></b>"; 
    $start1 = microtime(true); 
    $hash = password_hash($userPassword, PASSWORD_BCRYPT, ["cost" => $cost]); 
     echo "Hash is : " . $hash . "</br>"; 
     echo "Encryption took : ". (microtime(true) - $start1) ." seconds </br>"; 
     $start2 = microtime(true); 
     password_verify($userPassword, $hash); 
     echo "Password verification took : ". (microtime(true) - $start2) ." seconds </br></br>"; 

     $cost++; 

} while ($cost <= 16); 
$endpage = microtime(true); 

echo "The whole page took : ". ($endpage - $startpage) . " seconds </br>"; 
?> 

enter image description here

+3

是的。但是,如果使用PASSWORD_DEFAULT,那麼服務器升級可能會同時升級哈希算法。其實施這種方式來優雅地提升密碼哈希的安全性,當推薦使用新的算法而不是Bcrypt – JimL

+0

是的,並且可以用盡可能高的成本進行驗證 –

+0

這正是我以爲我在等待答案你的確認。用一個經典的i7 CPU 8G RAM暴力破解我的成本需要多長時間= 11實際的PASSWORD_DEFAULT? – shrimpdrake

回答

5

是,password_hash()是要走的路。安全StackExchange有一個偉大的職位,更多的信息here

使用bcrypt是一個好的開始;這是推薦的選擇(或至少推薦的選項之一)。 documentation似乎表明PHP 終於正確地處理事情。確保你的PHP版本至少是5.5.0。不要試圖擺弄鹽分:默認情況下,該功能會在需要時生成隨機鹽分,這是正確的做法,只要讓它做到它的工作即可。

你應該嘗試改變「成本」選項。對於bcrypt,成本是4到31之間的一個值;每個增量意味着密碼散列花費是貴兩倍,無論是爲您的服務器還是攻擊者。在實踐中,考慮到服務器功率,平均負載,峯值負載和最大用戶耐心等因素,您希望將該值設置爲可容忍的最高值:這將爲您提供最佳的安全性,這是您可以期待的。

(請注意,我說:「最好的」,而不是「好」。)

如果你想明白好的密碼哈希的基本概念,以及爲什麼bcrypt是一個不錯的選擇,start here

password_hash()已在PHP 5.5或以上版本有了很大的提高,這一點,與PASSWORD_BCRYPT一起,應該是一個很好的路要走。

相關問題