[英]How to change a SALT password in a database using PHP?
我正在使用從android到一些php腳本的HTTP POST,以更新數據庫中的用戶密碼。
我使用的SALT哈希與用戶創建帳戶且數據庫更新正在運行並更改SALT的值時使用的SALT哈希相同,但是,當我嘗試使用新密碼登錄時,它是不正確的。
創建密碼的初始代碼為:
public function storeUser($name, $email, $password, $rand) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$auth = 0;
$result = mysql_query("INSERT INTO users(unique_id, authorized, auth_code, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$auth', '$rand', '$name', '$email', '$encrypted_password', '$salt', NOW())");
// check for successful store
if ($result) {
// get user details
$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM users WHERE uid = $uid");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}
哈希函數是:
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
最后是更新功能(問題出在哪里):
/**
* Updating a users
* password
*/
public function updatePassword($email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysql_query("UPDATE users SET encrypted_password='$encrypted_password', updated_at = NOW() WHERE email='$email'");
$result = mysql_query("UPDATE users SET salt='$salt', updated_at = NOW() WHERE email='$email'");
// check for successful store
if ($result) {
// get user details
$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM users WHERE email = '$email'");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}
一如既往的任何幫助,我們將不勝感激。
編輯:
登錄功能要求:
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}
您建立了一個弱方案來對密碼進行哈希處理,這是因為您使用了SHA1的單個迭代作為哈希函數,並且由於鹽值被截斷為10個字符,並且是通過加密不安全的rand()
函數生成的。 然后,將鹽儲存在單獨的區域中會使您的生活變得比必要的更加艱難。
在您的情況下,我強烈建議您使用PHP的新函數password_hash()和password_verify()獲得強大的BCrypt哈希。 它們負責安全隨機鹽的生成,結果字符串包含哈希值和鹽,因此您可以將其存儲在數據庫的單個字段中。 存在用於早期PHP版本的兼容性包 。
使用此功能的最簡單方法是:
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);
$isPasswordCorrect = password_verify($password, $existingHashFromDb);
回答您的問題:我看不出為什么它不起作用的明顯原因,通常問題是數據庫字段太短。 在您的情況下,您需要放置添加了10個字符的鹽的二進制SHA1,該鹽以base64_encoded編碼(難以預測所需的大小)。 將SHA1中的二進制字符串(空字節)與普通字符串組合在一起也可能很危險。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.