PHP Classes

PHP Random Bytes: Generate cryptographically strong random bytes

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 174 All time: 8,748 This week: 190Up
Version License PHP version Categories
random-bytes 0.14GNU General Publi...5.4PHP 5, Cryptography, Security


This class can generate cryptographically strong random bytes.

It can generate a string of bytes with random values using a given method.

Currently it can use either methods using mcrypt, OpenSSL, or /dev/urandom on Linux. The default is to use mcrypt.

Innovation Award
PHP Programming Innovation award nominee
May 2015
Number 5

Prize: One downloadable e-book of choice by O'Reilly
Many cryptography algorithms use random numbers to make it harder to break the security measures based on those algorithms.

There are many pseudo-random number generators that can be used to generate cryptographically strong data values.

This class can generate random streams of bytes that are more secure to use in cryptography applications by either using /dev/random on Linux, OpenSSL or mcrypt libraries.

Manuel Lemos
Picture of Martin Latter
  Performance   Level  
Name: Martin Latter <contact>
Classes: 8 packages by
Country: United Kingdom
Age: ???
All time rank: 131361 in United Kingdom
Week rank: 70 Up2 in United Kingdom Up
Innovation award
Innovation award
Nominee: 5x


Random Bytes

Generate cryptographically-strong random bytes.


Create random bytes &ndash; as cryptographically-strong as possible &ndash; from available sources of entropy, and display in different output formats.

Crypto Sources

  • Linux/Unix: OpenSSL, `random_bytes()`, /dev/urandom
  • Windows: OpenSSL, `random_bytes()`

Random Bytes Definitions:

  • openssl
  • random_bytes
  • urandom



    array RandomBytes::generate(int $length, string $source);


    use Tinram\RandomBytes\RandomBytes;
    $aData = RandomBytes::generate(32, 'openssl');


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.

The random_bytes() function was added to PHP version 7.0


Random Bytes is released under the GPL v.3.

  Files folder image Files  
File Role Description
Files folder imageclasses (1 file)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file randombytes_example.php Example Example script
Accessible without login Plain text file randombytes_example2.php Aux. Auxiliary script
Accessible without login Plain text file Data Documentation

  Files folder image Files  /  classes  
File Role Description
  Plain text file randombytes.class.php Class Class source

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:8,748
This week:190Up