To generate a secure URL-safe hash for password reset or email activation, which one of these functions would be more secure, bin2hex or base64_encode in the following application?
1. Generate token (using one of these):
$token = bin2hex(openssl_random_pseudo_bytes(32));
$token = strtr(base64_encode(openssl_random_pseudo_bytes(64)), "+/=", "XXX");
2. Store in database:
$token_hash = password_hash($token, PASSWORD_BCRYPT);
INSERT INTO hash_table SET token_hash='$token_hash', series='X'
3. Validate token:
SELECT token_hash FROM hash_table WHERE series='X'
if (password_verify($hash_from_url,$token_hash)) {
// Success
}
I noticed that when $hash_from_url is more than 72 characters, it doesn't make a difference anymore.
But wouldn't it be better to use base64, to get 61 different characters rather than bin2hex that uses only 16, assuming I use a larger number of random bytes, for an equally long string, that is 72 characters of length?