Last active
August 29, 2015 14:13
-
-
Save Lewiscowles1986/0947d67b29c7dcee3037 to your computer and use it in GitHub Desktop.
fantastic secure random bytes from native PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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