Edit: This question is not about what adding salt to a hash is, how to do it, nor how it increases security.
I wrote the following code:
<?php
$salt = '';
$salt_alphabet = str_split('aAbBcCdDeEfFgGhHiIjJkKlLmM.nNoOpPqQrRsStTuUvVwWxXyYzZ/');
$salt_prefix = (version_compare(PHP_VERSION, '5.3.7') < 0) ? '$2a' : '$2y';
foreach (array_rand($salt_alphabet, 20) as $alphameric) {
$salt .= $salt_alphabet[$alphameric];
}
if (defined('CRYPT_BLOWFISH') and CRYPT_BLOWFISH) {
echo crypt('thisisasuperpassword', $salt_prefix . '$07$' . $salt . '$$');
}
?>
Which outputs a Blowfish hash like so:
$2y$07$acEfghiKL.OprtuwyYZ/$.j7uui28rXLPyAcDzGBfQbbvQL6a.kk2
If someone gets this hash, how is having the salt available in the hash itself secure?
People say Blowfish is the most secure encryption there is, but hash encryption like SHA-256 doesn't display what salt was used or not in the hash itself, which would seem to be more secure. Isn't the point of salt in hashing to add randomness to the hash, if you have the randomness as part of the hash, how can it still be more secure?
As with the hash above, "acEfghiKL.OprtuwyYZ/$" is the salt, plain as day, right there in the hash itself. All someone would have to do is add this salt to the values they already have in their rainbow table, hash database, or what have them, and there you go, the purpose of the salt is defeated.
Or is it not somehow?
As an example, once you load a database into an array all you'd have to do is:
<?php
foreach ($hashes as $hash) {
$salt = explode('$', $hash)[3];
// Proceed to use the salt to crack the hash
}
?>
This seems like a glaring security problem, how is it not?
Or what am I doing wrong?