Frage

I have two functions, HashPassword() and ValidatePassword.

The first one hashes the password given on signup form with a dynamic salt and the second validates the password.

Basically, I am checking if the password hashes match they never do match. On my login form when I call the ValidatePassword() function I have tested echoing out the hashes in the ValidatePassword() to make sure I am splitting the hash in write place for comparing and I am but when comparing them, they echo out a different hash.

Probably easier to look at both functions to explain better.

<?php

// hashes a users password along with a dynamic salt
// dynamic salt is stored with users password and is seperated by a ;
function HashPassword($password){
    // creates a dynamic salt
    $DynamicSalt       = uniqid('', true);
    // hash the password given from user along with dynamic salt
    $HashedPassword    = hash('sha512', $password . $DynamicSalt);
    // combine the hashed password seperated by ; then the dynamic salt to store in database
    $final             = $HashedPassword.';'.$DynamicSalt; // this value is stored in database like: c29fc9e4acdd2962c4db3f108bee728cf015c8f6388ab2cd4f21e405f9d2f13b2d53a1ab8629aa21c3453906a98aff0d4b9a0e14bfc2c553a4f9c7c0c32fc58a;4f91cfc746b426.53641182
    return $final;
}

// validate password user entered ($password = password from user | $dbHashedPassword = hash from database)
function ValidatePassword($password, $dbHashedPassword){
    // we need to get the password hash before the salt, (fetch just the first 128 characters from database hash)
    $CorrectHash = substr($dbHashedPassword, 0, 128);
    // get the dynamic salt from end of sha512 hash (
    $DynamicSalt = substr($dbHashedPassword, 129, 151); // get just the dynamic salt part of the db hash
    // hash the password from user and the dynamic salt which we got from the end of the hash from database
    $TestHash    = hash('sha512', $password . $DynamicSalt);

    return ($CorrectHash == $TestHash);


    // WHEN I ECHO OUT THE THREE VARIABLES $CorrectHash, $DynamicSalt and $TestHash
    // THE $CorrectHash (from db, first 128 chars) is not the same as $TestHash
    // AND TO MAKE SURE I AM SPLITTING THE HASH AND DYNAMIC SALT InN THE CORRECT PLACE I ECHO OUT
    // $DynamicSalt and it is split in the correct place showing the 23 characters which the dynamic salt is 23 characters
    // BUT WHEN I COMBINE THE $password and $DynamicSalt in $TestHash it shows a completely different hash from the $CorrectHash (which we got and split from database)
}

?>

I'm not sure what's wrong, but it seems I'm splitting the hash and dynamic salt in the correct place because when I echo out it shows the first 128 chars (sha512) then the dynamic salt (23 chars) but when echoing out the two 128 chars hashes they do not match (by this I mean they are completely different hashes).

War es hilfreich?

Lösung

It's probably something to do with how you're splitting the hash you're testing against. For instance, you're trying to get a salt that's 151 characters long.

Try this:

function ValidatePassword($password, $dbHashedPassword) {
    list($CorrectHash, $DynamicSalt) = explode(";",$dbHashedPassword,2);
    return $CorrectHash == hash("sha512",$password.$DynamicSalt);
}

Andere Tipps

if all your substr are correct you just forgot to append ";" . $DynamicSalt to the $TestHash

btw. this violates against the first database normalization rule: "values have to be atomic". the salt should be stored in a seperate field.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top