Generate cryptographically-strong random bytes.
Create random bytes - as cryptographically-strong as possible - from available sources of entropy, and display in different output formats.
- Linux and Unix: OpenSSL, /dev/urandom, random_bytes(), MCrypt
- Windows: OpenSSL, random_bytes(), MCrypt
require('randombytes.class.php'); use CopySense\RandomBytes\RandomBytes; $aData = RandomBytes::generate(32, 'openssl'); var_dump($aData);
The random bytes generated are only as good as the underlying entropy generator of the OS.
Linux's /dev/urandom entropy source is a non-blocking generator 'suitable for most cryptographic purposes'. (/dev/random, being blocking, isn't suitable for this script.)
OpenBSD and FreeBSD have non-blocking /dev/random implementations.
random_bytes() function was added to PHP version 7.0
On Windows, the implementation of MCrypt is known to have problems, which I confirmed as patterning present in images (instead of non-patterned noise) created from MCrypt-generated data (XAMPP, PHP 5.4).
The libmcrypt library behind MCrypt is unmaintained and contains unfixed bugs. MCrypt is now deprecated in PHP 7.1.
Random Bytes is released under the GPL v.3.