How can hashing be used to enhance the security of randomly generated strings in PHP?

When generating random strings in PHP, hashing can be used to enhance security by converting the random string into a fixed-length hash value that is unique to the input. This hashed value can then be securely stored or transmitted without revealing the original random string. By using a strong hashing algorithm like SHA-256, the random string's confidentiality and integrity can be protected.

// Generate a random string
$randomString = bin2hex(random_bytes(16));

// Hash the random string using SHA-256
$hashedString = hash('sha256', $randomString);

echo "Random String: " . $randomString . "\n";
echo "Hashed String: " . $hashedString;