检查 mysql 中的 SHA256 散列密码在登录表单中是否正确无效
Check if SHA256 hashed password in mysql is correct in login form not working
您好,我的代码有一个小问题。事情是,在 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
之类的内容。
您好,我的代码有一个小问题。事情是,在 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
之类的内容。