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    
Last Updated Ratings Unique User Downloads Download Rankings
2018-12-18 (5 months ago) RSS 2.0 feedNot enough user ratingsTotal: 168 All time: 8,520 This week: 350Up
Version License PHP version Categories
random-bytes 0.12GNU General Publi...5.4PHP 5, Cryptography, Security
Description Author

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
  Performance   Level  
Name: Martin Latter is available for providing paid consulting. Contact Martin Latter .
Classes: 7 packages by
Country: United Kingdom United Kingdom
Age: ???
All time rank: 160669 in United Kingdom United Kingdom
Week rank: 72 Up3 in United Kingdom United Kingdom Up
Innovation award
Innovation award
Nominee: 4x


Random Bytes

Generate cryptographically-strong random bytes.


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

Crypto Sources

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

Random Bytes Definitions:

  • openssl
  • random_bytes
  • mcrypt
  • urandom



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


    use CopySense\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

On Windows, the implementation of MCrypt is known to have problems, which can be confirmed by patterning present in images (instead of non-patterned noise) created from MCrypt-generated data.

The libmcrypt library behind MCrypt is unmaintained and contains unfixed bugs. MCrypt is deprecated in PHP 7.1.


Random Bytes is released under the GPL v.3.

  Files folder image Files  
File Role Description
Accessible without login Plain text file LICENSE Lic. License text
Plain text file randombytes.class.php Class Class source
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

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