Home > Dev > Crypto > Generating Secure Random Strings

Generating Secure Random Strings

This code snippet generates secure random strings for use as tokens or keys.

function generate_random_string($length = 32) {
    return bin2hex(random_bytes($length / 2));
}

// Usage example
$random_string = generate_random_string();
echo "Random String: " . $random_string . "\n";
Back