PHPackages                             elcodedocle/cryptosecureprng - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. elcodedocle/cryptosecureprng

ActiveClass[Utility &amp; Helpers](/categories/utility)

elcodedocle/cryptosecureprng
============================

cryptographically secure pseudo-random number generator class with an mt\_rand alike method interface

0.2.2(1y ago)34712MITPHPPHP &gt;=5.3.0

Since Sep 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/elcodedocle/cryptosecureprng)[ Packagist](https://packagist.org/packages/elcodedocle/cryptosecureprng)[ Docs](https://github.com/elcodedocle/cryptosecureprng)[ RSS](/packages/elcodedocle-cryptosecureprng/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (1)Versions (2)Used By (2)

cryptosecureprng
================

[](#cryptosecureprng)

##### *mt\_rand for the moderately paranoid citizen*

[](#mt_rand-for-the-moderately-paranoid-citizen)

Copyright (C) 2014 Gael Abadin
License: [MIT Expat](https://raw.githubusercontent.com/elcodedocle/cryptosecureprng/master/LICENSE) / beerware

### Motivation

[](#motivation)

Ever wanted a [crypto safe](http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) mt\_rand()? Not useful for many things I guess (maybe not useful at all), but I recently wanted to build a [class able to pick random words from a dictionary in a safe way](https://github.com/elcodedocle/chbspassgen), so they could be used as passwords ([Correct, horse. That's a battery staple](http://xkcd.com/936/)). I probably overdid it... The result is my attempt on a "cryptographically secure" PRNG implementation with an mt\_rand alike interface, including a wrapper for selecting and using the best random bytes generator from available extensions and /dev/urandom (about 100 times slower than mt\_rand on a single core, by the way, so clearly there is plenty of room for improvement if you want to fork it ;-))

### How to use

[](#how-to-use)

In a similar way as mt\_rand(), random integers are chosen from a given range following a uniform distribution:

```
require_once 'CryptoSecurePRNG.php';
$secGen =  new synapp\info\tools\passwordgenerator\cryptosecureprng\CryptoSecurePRNG();
$randInt = $secGen->rand(); //between 0 and mt_getrandmax()
$randInt = $secGen->rand(1,100); //between 1 and 100
$randInt = $secGen->rand(-50,50); //between -50 and 50
```

You can also get a string of random bytes:

```
require_once 'CryptoSecurePRNG.php';
$secGen =  new synapp\info\tools\passwordgenerator\cryptosecureprng\CryptoSecurePRNG();
$stringLength = 20; // number of random chars to be generated
$stringOfRandomChars = $secGen->getRandomBytesString($stringLength); // generate a string of $stringLength random ascii chars (non printable too)
```

And here is the code to visualize the output using matlab:

```
// PHP code, uses cryptosecureprng rand() to generate the samples
require_once 'CryptoSecurePRNG.php';
$prng = new synapp\info\tools\passwordgenerator\cryptosecureprng\CryptoSecurePRNG();
$out='';
for ($i=0;$irand(0,255).','.$prng->rand(0,255).',';
}
$fh = fopen('testout.txt','w');
fwrite ($fh, $out);
fclose($fh);
```

```
% Matlab code, reads and displays the generated samples
x=csvread('testout.txt');
C = reshape (x,720,1280,3);
C = uint8(C);
imwrite(C,'rgb_output.bmp');
hist(x,256);
saveas(gcf,'hist_output','png');
```

Check the code (or generate the docs using phpdocumentor) if you want more info on tweaks and available parameters.

If you like this class, feel free to buy me a beer ;-)

bitcoin: 15i9QKZqLuNdcyseHpjpZiPcty6FMazxk2

dogecoin: DCjimHzRu25smyjnEb7V9qFuVyf6P2JjBf

paypal:

Have fun.-

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity24

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

615d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/40f1df47e194d2a08e925e99e8bca6692f16195a8581c920280c01f1a560da4e?d=identicon)[elcodedocle](/maintainers/elcodedocle)

---

Top Contributors

[![elcodedocle](https://avatars.githubusercontent.com/u/3731026?v=4)](https://github.com/elcodedocle "elcodedocle (37 commits)")

---

Tags

opensslmcryptPRNGpseudo-random number generatorcrypto safe PRNGmt\_randurandom

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/elcodedocle-cryptosecureprng/health.svg)

```
[![Health](https://phpackages.com/badges/elcodedocle-cryptosecureprng/health.svg)](https://phpackages.com/packages/elcodedocle-cryptosecureprng)
```

###  Alternatives

[phpseclib/mcrypt_compat

PHP 5.x-8.x polyfill for mcrypt extension

28029.7M34](/packages/phpseclib-mcrypt-compat)[kelunik/certificate

Access certificate details and transform between different formats.

10938.3M8](/packages/kelunik-certificate)[ruafozy/mersenne-twister

Pure-PHP Mersenne Twister

221.4M12](/packages/ruafozy-mersenne-twister)[skywarth/chaotic-schedule

Randomize scheduled command execution time and date intervals

12142.3k](/packages/skywarth-chaotic-schedule)[savvot/random

Deterministic pseudo-random generators library with dozens of useful functions and several sources of randomness

21243.7k5](/packages/savvot-random)[delight-im/random

The most convenient way to securely generate anything random in PHP

2345.4k](/packages/delight-im-random)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
