Skip to content

Instantly share code, notes, and snippets.

@Lewiscowles1986
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lewiscowles1986/0947d67b29c7dcee3037 to your computer and use it in GitHub Desktop.
Save Lewiscowles1986/0947d67b29c7dcee3037 to your computer and use it in GitHub Desktop.
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