Skip to content

Instantly share code, notes, and snippets.

@Lewiscowles1986 Lewiscowles1986/Random.php
Last active Aug 29, 2015

Embed
What would you like to do?
fantastic secure random bytes from native PHP
<?php
namespace lewiscowles\security {
Class Random {
const SECRANDSRC = '/dev/urandom';
const DEFAULT_RANDOM_BYTES = 16;
public static function getTrueRandomBytes( $cnt=16 ) {
$out = '';
$cnt = intval( $cnt ) > 0 ? intval( $cnt) : self::DEFAULT_RANDOM_BYTES;
if( file_exists( self::SECRANDSRC ) && is_readable( self::SECRANDSRC ) ) {
$fp = fopen( self::SECRANDSRC, 'r' );
$out = fread( $fp, $cnt );
fclose( $fp );
} else {
throw new Exception('Don\'t Run a *nix system, don\'t use my code');
}
return $out;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.