簡體   English   中英

如何使用bcrypt與php進行密碼驗證?

[英]How to use bcrypt with php for password authentication?

我嘗試了如何在PHP中使用bcrypt進行散列密碼的頂級解決方案帖子 但似乎無法得到一個工作的例子。 我復制了Bcrypt類,並在其底部添加了以下代碼。

$bcrypt = new Bcrypt(15);

// pw on server. Used $pwHash = $bcrypt->hash($formPassword); to get the hash from 'qwerty'.
$serverPw = '$2a$15$Ty6hIEEWFpUFHoKujvdmw.9kmyrwYip2s8TLdjDfNoVJuQx/TGgwu'; 

// user enters plain text pw...
$passAttempt = 'qwerty';

// attempt to check the attempted password against the server hashed pasword.
$pwVerify = $bcrypt->verify($serverPw, $passAttempt); 

if ( $pwVerify == 1 ) {echo "$pwVerify = true";} else {echo "$pwVerify = not true";}
// I also tried if ($pwVerify) and if ($bcrypt->verify($serverPw, $passAttempt))
// Output is "= not true"

這有什么不對?

您必須存儲密碼和BCrypt時使用的鹽,否則您將永遠不會獲得相同的字符串。 這個課對我來說似乎很糟糕,不要使用它。 請參閱此示例文檔以直接使用PHP的crypt函數。

編輯:您可能應該使用PHPPass ,看起來像一個經過良好測試和引用的庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM