Hi folks,
I am taking a course in PHP and MYSQL. I’M A NEWBIE. I have an assignment that I’m working on that should be simple.
I have constructed the following(see below) based on class information I was suppose to use. I could choose my own password, my hash format(Blowfish) and salt length.
I randomly generated a salt of 22 characters, concatenated the hash format and salt, then applied the crypt function to the password and the concatenated hash and salt.
What I expected from my crypt function was to see the hash format followed by 22 characters of the randomly generated salt, followed by the encrypted password. For some reason one character at the end of the salt is being removed and I don’t know why. Any thoughts?
Here is my code:
<?php $password = "secret2"; $hash_format = "$2y$10$"; // Tells PHP to use Blowfish with a "cost" of 10 $salt_length = 22; // Blowfish salts should be 22-characters or more $unique_random_string = md5(uniqid(mt_rand(), true)); // Valid characters for a salt are [a-zA-Z0-9./] echo $unique_random_string; echo ""; $base64_string = base64_encode($unique_random_string); // But not '+' which is valid in base64 encoding echo $base64_string; echo "
"; $modified_base64_string = str_replace('+', '.', $base64_string); echo $modified_base64_string; echo "
"; // Truncate string to the correct length $salt = substr($modified_base64_string, 0, $salt_length); echo "random generated salt, " . $salt . " , with string length of " . strlen($salt) . " characters."; echo "
"; $hash_format_and_salt = $hash_format . $salt; echo "
"; echo "hash format and random salt concatenated: " . $hash_format_and_salt; $hash = crypt($password,$hash_format_and_salt); echo "
"; echo "hash generated with crypt, password and salt : " . $hash; echo "
"; echo "Notice the hash format followed by the salt which is missing its last character followed by the encrypted password. Why is the last letter of the salt missing from the hash?"; ?>
Thanks for your time and patience,
Greg