我在這裏有一些用於登錄系統的代碼,純粹是爲了學習的目的,它是由來自stackoverflow的偉大人物的一些主要幫助創建的,並且我被告知不要存儲salt和hash分離,而是一起存儲。我想知道如何在用戶嘗試登錄時比較密碼。如果鹽不存儲,我怎麼能比較這兩個。誰能幫忙?如何比較使用此代碼登錄的用戶密碼?
require("constants.php");
$DBH = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
function createSalt() {
$length = mt_rand(64, 128);
$salt = '';
for ($i = 0; $i < $length; $i++) {
$salt .= chr(mt_rand(33, 255));
}
return $salt;
}
//Salt function created by ircmaxell
function registerNewUser() {
//Check to see if Username Is In Use//
$q = $DBH->prepare("SELECT id FROM users WHERE username = ?");
$username = filter_var($username, FILTER_SANITIZE_STRING);
$data = array($username);
$q->execute($data);
$row = $q->fetch();
if ($row === false) {
//If Username Is Not Already In Use Insert Data//
$hash = hash('sha256', $pass);
$salt = createSalt();
$hash = hash('sha256', $salt . $hash . $pass); //UPDATED
$data = array($username, $hash, $salt);
$qInsert = $DBH->prepare(
"INSERT INTO users (username, password, salt) values (?, ?, ?)"
);
$qInsert->execute($data); //Inserts User Data Into Table//
}
}
你可以安全地跳過'$ hash = hash('sha256',$ pass);' – Jacco 2010-12-16 09:36:31