2013-08-30 54 views
-3

registeration期間,我散列和鹽密碼如下密碼SHA散列登錄

// Create a random salt 
$salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true)); 
// Create salted password (Careful not to over season) 
$pass = hash('sha512', $pass.$salt); 

鹽和通被存儲在數據庫中。但我不知道如何使用哈希和鹽漬密碼創建登錄表單。任何人都可以幫助我下面是我的登錄表格

//session_start(); 

if (
    isset($_POST['username']) && 
    isset($_POST['pass']) && 
    !empty($_POST['username']) && 
    !empty($_POST['pass']) 

) { 

    $db = new PDO(
     'mysql:host=localhost;dbname=timeline', // dsn 

     'root', // username 
     '' // password 
    ); 


    $statement = $db->prepare(' 
     SELECT * FROM users 
     WHERE username = ? 
     AND password = ? 

    '); 

    $statement->execute(array(
     $_POST['username'], 
     $_POST['pass'] 

    )); 

    if ($row = $statement->fetch()) { 

     $_SESSION['sess_user'] = $row['uid']; 

     $_SESSION['sess_name'] = $row['username']; 
     include('success.php'); 


    } else include('fail.php'); 

} else include('fail.php'); 
+0

我們需要更多信息:鹽在哪裏?散列在哪裏? –

+1

您應該使用新的[密碼散列擴展](http://php.net/manual/en/book.password.php)。如果您的PHP爲<5.5.0,請使用[compatibility library](https://github.com/ircmaxell/password_compat)。另外,你需要更詳細地解釋你正在嘗試做什麼 - 這一點目前還不太清楚。 –

回答

0

你必須將鹽與用戶名和密碼散列一起存儲在數據庫中。

查詢數據庫(使用用戶名上的WHERE條件)來檢索salt和密碼哈希。計算密碼哈希,並將其與您從數據庫檢索到的密碼哈希進行比較。


進行恆定時間比較的獎勵積分。

+0

這只是我的問題,你能不能在查詢語句中給出例子,請 – mutago