1

Hello I have a small problem with my code. The thing is that in minecraft server the user is asked to register and then his registration details go into mysql database and password is encrypted in SHA256 custom hash. Here is its algorithm:

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

The problem is that I need to people to login to my website using that password and their username. But in order to check it I need to encode mine password and check if they are the same. But my code does not seem to work, I've tried everything. please help me. Thank you!!:

    <?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();
?>

1 Answer 1

3
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;
}

You're passing a single variable into the function, so change all references of $row['password'] to something like $db_password for the function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.