0

I used the crypt function with the blowfish algorithm(one-way hashing)and i saved my hashed string in db there no problem.

$hash=$2y$13$1wVxPniVSiKTjBmDxUhykeec08.v0UsujEkmhjHECIUgEiSuJFag 
 $actual=crypt("kumar",$hash);

These is how they used to validate the password is to took our current password and the hashed password which we can stored into the db.

On during these they compared with follwing code

 public function compareString($expected, $actual)
    {
        $expected .= "\0";
        $actual .= "\0";
        $expectedLength = StringHelper::byteLength($expected);
        $actualLength = StringHelper::byteLength($actual);
        $diff = $expectedLength - $actualLength;
        for ($i = 0; $i < $actualLength; $i++) {
            $diff |= (ord($actual[$i]) ^ ord($expected[$i % $expectedLength]));
        }
        return $diff === 0;
    }

It return only 0 or 1 when the password is correct or not.

But My question is on these how they matched the current password with the hashed password which we save in db.Because the current password only contains the string and the hashed password contains the cost,salt,hashed password.

Is that they validate the password only or only the salt or how they do it?

I want the flow of validation of passwords of In-built crypt algorithm.

1615903
  • 32,635
  • 12
  • 70
  • 99
neevan
  • 31
  • 8

1 Answers1

2

For a starter I would recommend to use the functions password_hash() and password_verify() to check passwords, internally they use the crypt() function.

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);

But you seem to be interested in how the crypt() is able to verify the password with its hash. When you create the first hash, you pass in the crypt-parameters as the second argument, it contains the algorithm, the cost factor and the salt (the format is explained in this answer).

For verification you can calculate the hash again, but you need the exact same crypt-parameters, then you get a comparable hash. The first hash starts with the crypt-parameters, and the crypt() function extracts those parameters from the first hash, when you pass it as the second argument.

$2y$13$1wVxPniVSiKTjBmDxUhyke

In your example this part of the hash contains the crypt-parameters, it is the start of your hash and it is used by crypt() again to verify the password.

Community
  • 1
  • 1
martinstoeckli
  • 23,430
  • 6
  • 56
  • 87
  • @martinstoeckli--Thanks for your answer,But i have a small question you said that (the crypt() function extracts those parameters from the first hash).You mention the word extract which means they took all our(first 29 char which is hash+cost+salt) from the already hashed password and only crypt the password and send for validation.Is that am correct? – neevan Mar 16 '17 at 16:29
  • @neevan - Yes you understood it correctly. It takes the first 29 characters (if it is a BCrypt hash) and uses those to calculate a comparable hash. The resulting hash can then be compared with the first hash. This is more or less what the function password_verify() does. – martinstoeckli Mar 16 '17 at 17:10
  • @martinstoeckli---ya thanks for your help it really helps more me to do my project.Is that can you have the In-Built coding of the crypt function of the validation Part .Please share me if you have – neevan Mar 16 '17 at 17:39
  • @neevan - As far as I know, it finally calls the Unix crypt tool, but I'm not sure about that. – martinstoeckli Mar 16 '17 at 21:01
  • @martinstoeckli--In the first comment we discuss about that they (extract 29 char)Then what about the compare string in these code. – neevan Mar 17 '17 at 11:50
  • https://github.com/yiisoft/yii2/blob/master/framework/base/Security.php these link contains the code – neevan Mar 17 '17 at 12:00
  • @neevan - This string comparison is an attempt to prevent _timing attacks_, each comparison needs the same amount of time. Since we are comparing hashes and not the passwords, and because cryptographic hashes do not allow conclusions about the original password, this is acually not necessary. On the other side it surely doesn't hurt. – martinstoeckli Mar 19 '17 at 18:48