src/Yubikey.php
protected static function getRandomBytes($num = 16)
    {
        static $which = null;
        if ($which === null) {
            if (\function_exists('\\random_bytes') && \version_compare(\phpversion(), '7.0.0', '>=')) {
                $which = 'php7';
            } elseif (\function_exists('\\openssl_random_pseudo_bytes')) {
                $which = 'openssl';
            } elseif (\function_exists('\\mcrypt_create_iv')) {
                $which = 'mcrypt';
            } elseif (\is_readable('/dev/urandom')) {
                $which = 'urandom';
            } else {
                $which = 'fallback';
            }
        }

        if ($num < 1 || $num > PHP_INT_MAX) {
            return false;
        }

        // Now let's get some random bytes
        switch ($which) {
            case 'php7':
                return \random_bytes($num);
            case 'mcrypt':
                return \mcrypt_create_iv($num, MCRYPT_DEV_URANDOM);
            case 'openssl';
                return \openssl_random_pseudo_bytes($num);
            case 'urandom':
                $fp = \fopen('/dev/urandom', 'rb');
                \stream_set_read_buffer($fp, 0);
                $bytes = \fread($fp, $num);
                \fclose($fp);
                return $bytes;
            default:
                // I really hope this is never necessary
                $bytes = '';
                for ($i = 0; $i < $num; ++$i) {
                    $bytes .= \chr(\mt_rand(0, 255) ^ \rand(0, 255));
                }

                $xorbuf = \sha1(\json_encode($_SERVER), true);
                while (self::safeStrlen($xorbuf) < $num) {
                    $xorbuf .= \sha1(
                        \uniqid(
                            \md5(
                                \microtime(true) . \lcg_value()
                            ),
                            true
                        ),
                        true
                    );
                }
                for ($i = 0; $i < $num; ++$i) {
                    $bytes[$i] ^= $xorbuf[$i];
                }

                return $bytes;
        }
    }
protected static function getRandomBytes($num = 16)
    {
        static $which = null;
        if ($which === null) {
            if (\function_exists('\\random_bytes') && \version_compare(\phpversion(), '7.0.0', '>=')) {
                $which = 'php7';
            } elseif (\function_exists('\\openssl_random_pseudo_bytes')) {
                $which = 'openssl';
            } elseif (\function_exists('\\mcrypt_create_iv')) {
                $which = 'mcrypt';
            } elseif (\is_readable('/dev/urandom')) {
                $which = 'urandom';
            } else {
                $which = 'fallback';
            }
        }

        if ($num < 1 || $num > PHP_INT_MAX) {
            return false;
        }

        // Now let's get some random bytes
        switch ($which) {
            case 'php7':
                return \random_bytes($num);
            case 'mcrypt':
                return \mcrypt_create_iv($num, MCRYPT_DEV_URANDOM);
            case 'openssl';
                return \openssl_random_pseudo_bytes($num);
            case 'urandom':
                $fp = \fopen('/dev/urandom', 'rb');
                \stream_set_read_buffer($fp, 0);
                $bytes = \fread($fp, $num);
                \fclose($fp);
                return $bytes;
            default:
                // I really hope this is never necessary
                $bytes = '';
                for ($i = 0; $i < $num; ++$i) {
                    $bytes .= \chr(\mt_rand(0, 255) ^ \rand(0, 255));
                }

                $xorbuf = \sha1(\json_encode($_SERVER), true);
                while (self::safeStrlen($xorbuf) < $num) {
                    $xorbuf .= \sha1(
                        \uniqid(
                            \md5(
                                \microtime(true) . \lcg_value()
                            ),
                            true
                        ),
                        true
                    );
                }
                for ($i = 0; $i < $num; ++$i) {
                    $bytes[$i] ^= $xorbuf[$i];
                }

                return $bytes;
        }
    }
protected static function getRandomBytes ( $ num = 16 ) { static $ which = null ; if ( $ which === null ) { if ( \ function_exists ( '\\random_bytes' ) && \ version_compare ( \ phpversion ( ) , '7.0.0' , '>=' ) ) { $ which = 'php7' ; } elseif ( \ function_exists ( '\\openssl_random_pseudo_bytes' ) ) { $ which = 'openssl' ; } elseif ( \ function_exists ( '\\mcrypt_create_iv' ) ) { $ which = 'mcrypt' ; } elseif ( \ is_readable ( '/dev/urandom' ) ) { $ which = 'urandom' ; } else { $ which = 'fallback' ; } } if ( $ num < 1 || $ num > PHP_INT_MAX ) { return false ; } // Now let's get some random bytes switch ( $ which ) { case 'php7' : return \ random_bytes ( $ num ) ; case 'mcrypt' : return \ mcrypt_create_iv ( $ num , MCRYPT_DEV_URANDOM ) ; case 'openssl' ; return \ openssl_random_pseudo_bytes ( $ num ) ; case 'urandom' : $ fp = \ fopen ( '/dev/urandom' , 'rb' ) ; \ stream_set_read_buffer ( $ fp , 0 ) ; $ bytes = \ fread ( $ fp , $ num ) ; \ fclose ( $ fp ) ; return $ bytes ; default : // I really hope this is never necessary $ bytes = '' ; for ( $ i = 0 ; $ i < $ num ; ++ $ i ) { $ bytes .= \ chr ( \ mt_rand ( 0 , 255 ) ^ \ rand ( 0 , 255 ) ) ; } $ xorbuf = \ sha1 ( \ json_encode ( $ _SERVER ) , true ) ; while ( self :: safeStrlen ( $ xorbuf ) < $ num ) { $ xorbuf .= \ sha1 ( \ uniqid ( \ md5 ( \ microtime ( true ) . \ lcg_value ( ) ) , true ) , true ) ; } for ( $ i = 0 ; $ i < $ num ; ++ $ i ) { $ bytes [ $ i ] ^= $ xorbuf [ $ i ] ; } return $ bytes ; } }
Get a string of cryptographically secure pseudorandom bytes

@param int $num
@return string
@throws \Exception
Get a string of cryptographically secure pseudorandom bytes