random_bytes to generate cryptographically secure random bytes
echo random_bytes($length);
$length : Length of the random string returned as bytes.
Generates cryptographically secure pseudo-random bytes which can be used as Salts, keys etc.
Available in PHP 7
Example
$str=random_bytes(7);
echo bin2hex($str);
Output ( Refresh this page to to see how the random number changes )
5597acd1976fa8
Example 1: Generating Cryptographically Secure API Keys
<?php
$key = bin2hex(random_bytes(16)); // Generates a 32-character API key
echo "Generated API key: $key";
?>
Example 2: Creating Random Passwords
<?php
$password = bin2hex(random_bytes(8)); // Generates a secure 16-character password
echo "Random password: $password";
?>
Example 3: Generating Salt for Hashing
<?php
$salt = bin2hex(random_bytes(16)); // 16-byte salt for hashing
echo "Generated salt: $salt";
?>
← String Functions how to change all the letters to upper case →
nl2br() To add Line break →
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com