Skip to content

Kengaming1st/Secure-PHP-Random-Bytes

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Secure PHP random bytes
by @shoghicp

Useful for generating strong keys or random data. Better than default PHP random functions. Lots of different sources of entropy


Basic Usage: string randomBytes( [ int $length = 16 [, bool $secure = true [, bool $raw = true [, mixed $startEntropy = "" [, &$rounds [, &$drop ]]]]]])

	$length = 16;
	$bytes = randomBytes($length, true, false); //This will return 32 secure hexadecimal characters
	$bytes = randomBytes($length, false, true); //This will return 16 binary characters


	
Randomness tests using ENT (http://www.fourmilab.ch/random/)
	Machine: Windows 7 SP1 Intel i5 2.4 GHz x64 PHP 5.4.3, So no /dev/urandom
	
	USING SECURE MODE
	Generator code: randomBytes(524288, true, true, "", $rounds, $drop);
	Output:
		Entropy = 7.999653 bits per byte.

		Optimum compression would reduce the size
		of this 524288 byte file by 0 percent.

		Chi square distribution for 524288 samples is 252.21, and randomly
		would exceed this value 53.77 percent of the times.

		Arithmetic mean value of data bytes is 127.3803 (127.5 = random).
		Monte Carlo value for Pi is 3.144482210 (error 0.09 percent).
		Serial correlation coefficient is -0.002599 (totally uncorrelated = 0.0).
		
	USING NON-SECURE MODE
	Generator code: randomBytes(1048576, false, true, "", $rounds, $drop);
	Output:
		Entropy = 7.999840 bits per byte.

		Optimum compression would reduce the size
		of this 1048576 byte file by 0 percent.

		Chi square distribution for 1048576 samples is 232.65, and randomly
		would exceed this value 83.90 percent of the times.

		Arithmetic mean value of data bytes is 127.5482 (127.5 = random).
		Monte Carlo value for Pi is 3.139217908 (error 0.08 percent).
		Serial correlation coefficient is -0.002567 (totally uncorrelated = 0.0).
		
	
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

About

Useful for generating strong keys or random data. Better than default PHP random functions. Lots of different sources of entropy

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%