我正在我的 android 应用程序中实现密码更改功能,并在我的 php 文件中编码了密码哈希。用户可以更改密码,密码存储在数据库中。当我尝试使用电子邮件和新密码登录时,它告诉我密码错误。我在哪里为我的 php 文件做错了?
这是我的 php 文件代码:
<?php
// array for JSON response
$response = array();
function hashSSHA($newpassword) {
$salt = mhash('sha512', rand());
$salt = substr($salt, 0, 15);
$encrypted = hash('sha512', $newpassword . $salt, true) . $salt;
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
// check for required fields
if (isset($_POST['email']) && isset($_POST['newpassword'])) {
$email = $_POST['email'];
$newpassword = $_POST['newpassword'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// TESTING HERE FOR STORING NEW PASSWORD INTO DATABASE
$hash = hashSSHA($newpassword);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysql_query("UPDATE users SET encrypted_password = '$encrypted_password', salt = '$salt' WHERE email = '$email'");
// check if row inserted or not
if ($result) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Password successfully changed";
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Password change failed";
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
编辑 这是我的解密功能
// DECRYPTING user currentpassword
function checkhashSSHA($salt, $currentpassword) {
$hash = hash('sha512', $currentpassword . $salt, true) . $salt;
return $hash;
}