X-Git-Url: https://git.stricted.de/?p=GitHub%2FStricted%2Fspeedport-hybrid-php-api.git;a=blobdiff_plain;f=CryptLib%2FRandom%2FSource%2FOpenSSL.php;fp=CryptLib%2FRandom%2FSource%2FOpenSSL.php;h=87dd333d701a57ec43dc168d292f5d0b8ea7e2a3;hp=0000000000000000000000000000000000000000;hb=14d4f286d33b631a93207e4d13086c0a3bc0df12;hpb=c9e082da5cc662b64a74d3770f13d1270068678f diff --git a/CryptLib/Random/Source/OpenSSL.php b/CryptLib/Random/Source/OpenSSL.php new file mode 100644 index 0000000..87dd333 --- /dev/null +++ b/CryptLib/Random/Source/OpenSSL.php @@ -0,0 +1,64 @@ + + * @copyright 2011 The Authors + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version Build @@version@@ + */ + +namespace CryptLib\Random\Source; + +use CryptLib\Core\Strength; + +/** + * The OpenSSL Random Number Source + * + * This uses the OS's secure generator to generate high strength numbers + * + * @category PHPCryptLib + * @package Random + * @subpackage Source + * @author Anthony Ferrara + * @codeCoverageIgnore + */ +class OpenSSL implements \CryptLib\Random\Source { + + /** + * Return an instance of Strength indicating the strength of the source + * + * @return Strength An instance of one of the strength classes + */ + public static function getStrength() { + return new Strength(Strength::HIGH); + } + + /** + * Generate a random string of the specified size + * + * @param int $size The size of the requested random string + * + * @return string A string of the requested size + */ + public function generate($size) { + if (!function_exists('openssl_random_pseudo_bytes') || $size < 1) { + return str_repeat(chr(0), $size); + } + /** + * Note, normally we would check the return of of $crypto_strong to + * ensure that we generated a good random string. However, since we're + * using this as one part of many sources a low strength random number + * shouldn't be much of an issue. + */ + return openssl_random_pseudo_bytes($size); + } + +}