gpt4 book ai didi

php - 检查 mysql 中的 SHA256 哈希密码在登录表单中是否正确不起作用

转载 作者:行者123 更新时间:2023-11-29 05:16:11 37 4
gpt4 key购买 nike

您好,我的代码有一个小问题。事情是,在 minecraft 服务器中,用户被要求注册,然后他的注册详细信息进入 mysql 数据库,密码在 SHA256 自定义哈希中加密。这是它的算法:

    String salt = randomString(length:16);
String encryptedPassword = "$SHA$" + salt + "$" + sha256(sha256(password) + salt);

问题是我需要人们使用该密码和他们的用户名登录我的网站。但是为了检查它,我需要对我的密码进行编码并检查它们是否相同。但是我的代码似乎不起作用,我已经尝试了一切。请帮我。谢谢!!:

    <?php
require_once 'config.php';
$mysqli = new mysqli($hostname, $user, $pass, $db4);
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

$name = 'name';
$password = 'password';
$v = $mysqli->query("SELECT password FROM authme WHERE username = '".$name."'");
if ($v->num_rows > 0) {
$row = $v->fetch_assoc();
echo $row['password'];
}
else {
echo 'There is no user with such name';
exit();
}
function checkPassword($password,$row['password']){
$parts = explode('$',$row['password']);
$salt = $parts[2];
$hashed = hash('sha256',hash('sha256', $password).$salt);
$hashed = '$SHA$'.$salt.'$'.$hashed;
if ($hashed == $row['password'])
{
return true;
}
else {
return false;
}
}
if (checkPassword($password, $row['password']) === true) {
echo 'OMGGGGGGGGGGGGGGGGGGGG';
}
else {
echo 'something went wrong';
}
$mysqli->close();
?>

最佳答案

function checkPassword($password, $db_password)
{
$parts = explode('$', $db_password);
$salt = $parts[2];
$hashed = hash('sha256',hash('sha256', $password).$salt);
$hashed = '$SHA$'.$salt.'$'.$hashed;
return ($hashed == $db_password) ? true : false;
}

您将单个变量传递到函数中,因此将 $row['password'] 的所有引用更改为类似 $db_password 的函数。

关于php - 检查 mysql 中的 SHA256 哈希密码在登录表单中是否正确不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32426890/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com