src/StringUtils.php
public static function getRandomBytes($byteLength)
    {
        $results = null;

        if (function_exists('openssl_random_pseudo_bytes')) {
            /** @noinspection CryptographicallySecureRandomnessInspection */
            $results = openssl_random_pseudo_bytes($byteLength);
        } elseif (is_readable('/dev/urandom')) {
            $fileHandler = fopen('/dev/urandom', 'rb');

            if ($fileHandler !== false) {
                $results = fread($fileHandler, $byteLength);

                fclose($fileHandler);
            }
        } elseif (function_exists('mcrypt_create_iv')
                  && version_compare(PHP_VERSION, '5.3.0', '>=')
        ) {
            /** @noinspection CryptographicallySecureRandomnessInspection */
            /** @noinspection PhpDeprecationInspection */
            $results = mcrypt_create_iv($byteLength, MCRYPT_DEV_RANDOM);
        } elseif (function_exists('random_bytes')) {
            $results = random_bytes($byteLength);
        } elseif (class_exists('COM')) {
            /** @noinspection BadExceptionsProcessingInspection */
            try {
                /** @noinspection PhpUndefinedClassInspection */
                /** @noinspection SpellCheckingInspection */
                $capiUtil = new \COM('CAPICOM.Utilities.1');
                /** @noinspection PhpUndefinedMethodInspection */
                /** @noinspection SpellCheckingInspection */
                $results = $capiUtil->GetRandom($byteLength, 0);
            } catch (\Exception $ex) {
            } // Fail silently
        }

        /** @noinspection IsEmptyFunctionUsageInspection */
        if (empty($results)) {
            throw new Exception\RuntimeException(
                'Unable to find a secure method for generating random bytes.'
            );
        }

        return $results;
    }
public static function getRandomBytes($byteLength)
    {
        $results = null;

        if (function_exists('openssl_random_pseudo_bytes')) {
            /** @noinspection CryptographicallySecureRandomnessInspection */
            $results = openssl_random_pseudo_bytes($byteLength);
        } elseif (is_readable('/dev/urandom')) {
            $fileHandler = fopen('/dev/urandom', 'rb');

            if ($fileHandler !== false) {
                $results = fread($fileHandler, $byteLength);

                fclose($fileHandler);
            }
        } elseif (function_exists('mcrypt_create_iv')
                  && version_compare(PHP_VERSION, '5.3.0', '>=')
        ) {
            /** @noinspection CryptographicallySecureRandomnessInspection */
            /** @noinspection PhpDeprecationInspection */
            $results = mcrypt_create_iv($byteLength, MCRYPT_DEV_RANDOM);
        } elseif (function_exists('random_bytes')) {
            $results = random_bytes($byteLength);
        } elseif (class_exists('COM')) {
            /** @noinspection BadExceptionsProcessingInspection */
            try {
                /** @noinspection PhpUndefinedClassInspection */
                /** @noinspection SpellCheckingInspection */
                $capiUtil = new \COM('CAPICOM.Utilities.1');
                /** @noinspection PhpUndefinedMethodInspection */
                /** @noinspection SpellCheckingInspection */
                $results = $capiUtil->GetRandom($byteLength, 0);
            } catch (\Exception $ex) {
            } // Fail silently
        }

        /** @noinspection IsEmptyFunctionUsageInspection */
        if (empty($results)) {
            throw new Exception\RuntimeException(
                'Unable to find a secure method for generating random bytes.'
            );
        }

        return $results;
    }
public static function getRandomBytes ( $ byteLength ) { $ results = null ; if ( function_exists ( 'openssl_random_pseudo_bytes' ) ) { /** @noinspection CryptographicallySecureRandomnessInspection */ $ results = openssl_random_pseudo_bytes ( $ byteLength ) ; } elseif ( is_readable ( '/dev/urandom' ) ) { $ fileHandler = fopen ( '/dev/urandom' , 'rb' ) ; if ( $ fileHandler !== false ) { $ results = fread ( $ fileHandler , $ byteLength ) ; fclose ( $ fileHandler ) ; } } elseif ( function_exists ( 'mcrypt_create_iv' ) && version_compare ( PHP_VERSION , '5.3.0' , '>=' ) ) { /** @noinspection CryptographicallySecureRandomnessInspection */ /** @noinspection PhpDeprecationInspection */ $ results = mcrypt_create_iv ( $ byteLength , MCRYPT_DEV_RANDOM ) ; } elseif ( function_exists ( 'random_bytes' ) ) { $ results = random_bytes ( $ byteLength ) ; } elseif ( class_exists ( 'COM' ) ) { /** @noinspection BadExceptionsProcessingInspection */ try { /** @noinspection PhpUndefinedClassInspection */ /** @noinspection SpellCheckingInspection */ $ capiUtil = new \ COM ( 'CAPICOM.Utilities.1' ) ; /** @noinspection PhpUndefinedMethodInspection */ /** @noinspection SpellCheckingInspection */ $ results = $ capiUtil -> GetRandom ( $ byteLength , 0 ) ; } catch ( \ Exception $ ex ) { } // Fail silently } /** @noinspection IsEmptyFunctionUsageInspection */ if ( empty ( $ results ) ) { throw new Exception \ RuntimeException ( 'Unable to find a secure method for generating random bytes.' ) ; } return $ results ; }
Returns X random raw binary bytes.

@param int $byteLength

@return string
@throws \Xloit\Std\Exception\RuntimeException
Returns X random raw binary bytes .