2017 © Pedro Peláez
 

class cryptosecureprng

cryptographically secure pseudo-random number generator class with an mt_rand alike method interface

image

elcodedocle/cryptosecureprng

cryptographically secure pseudo-random number generator class with an mt_rand alike method interface

  • Monday, February 19, 2018
  • by elcodedocle
  • Repository
  • 1 Watchers
  • 2 Stars
  • 12 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 20 % Grown

The README.md

cryptosecureprng

mt_rand for the moderately paranoid citizen

Copyright (C) 2014 Gael Abadinbr/ License: MIT Expat / beerwarebr/, (*1)

Motivation

Ever wanted a crypto safe 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, so they could be used as passwords (Correct, horse. That's a battery staple). 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 ;-)), (*2)

How to use

In a similar way as mt_rand(), random integers are chosen from a given range following a uniform distribution:, (*3)

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:, (*4)

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:, (*5)

// 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;$i<1280;$i++) for ($j=0;$j<720;$j++) { 
  $out .= $prng->rand(0,255).','.$prng->rand(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., (*6)

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

bitcoin: 15i9QKZqLuNdcyseHpjpZiPcty6FMazxk2, (*8)

dogecoin: DCjimHzRu25smyjnEb7V9qFuVyf6P2JjBf, (*9)

paypal: http://goo.gl/iQd1UL, (*10)

Have fun.-, (*11)

The Versions

19/02 2018

dev-master

9999999-dev https://github.com/elcodedocle/cryptosecureprng

cryptographically secure pseudo-random number generator class with an mt_rand alike method interface

  Sources   Download

MIT MIT Expat

The Requires

  • php >=5.3.0

 

openssl mcrypt prng pseudo-random number generator crypto safe prng mt_rand urandom

19/02 2018

0.2.2

0.2.2.0 https://github.com/elcodedocle/cryptosecureprng

cryptographically secure pseudo-random number generator class with an mt_rand alike method interface

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

openssl mcrypt prng pseudo-random number generator crypto safe prng mt_rand urandom

12/10 2015

0.2.2-beta

0.2.2.0-beta https://github.com/elcodedocle/cryptosecureprng

cryptographically secure pseudo-random number generator class with an mt_rand alike method interface

  Sources   Download

MIT Expat

The Requires

  • php >=5.3.0

 

openssl mcrypt prng pseudo-random number generator crypto safe prng mt_rand urandom

28/03 2015

0.2.1-beta

0.2.1.0-beta https://github.com/elcodedocle/cryptosecureprng

cryptographically secure pseudo-random number generator class with an mt_rand alike method interface

  Sources   Download

MIT Expat

The Requires

  • php >=5.3.0

 

openssl mcrypt prng pseudo-random number generator crypto safe prng mt_rand urandom

30/06 2014

0.2.0-beta

0.2.0.0-beta https://github.com/elcodedocle/cryptosecureprng

cryptographically secure pseudo-random number generator class with an mt_rand alike method interface

  Sources   Download

MIT Expat

The Requires

  • php >=5.3.0

 

openssl mcrypt prng pseudo-random number generator crypto safe prng mt_rand urandom