wpseek.com
A WordPress-centric search engine for devs and theme authors



random_bytes › WordPress Function

Sincen/a
Deprecatedn/a
random_bytes ( $bytes )
Parameters:
  • (int) $bytes
    Required: Yes
Returns:
  • (string)
Defined at:
Codex:

Powered by ext/mcrypt (and thankfully NOT libmcrypt)



Source

function random_bytes($bytes)
    {
        try {
            /** @var int $bytes */
            $bytes = RandomCompat_intval($bytes);
        } catch (TypeError $ex) {
            throw new TypeError(
                'random_bytes(): $bytes must be an integer'
            );
        }

        if ($bytes < 1) {
            throw new Error(
                'Length must be greater than 0'
            );
        }

        /** @var string|bool $buf */
        $buf = @mcrypt_create_iv((int) $bytes, (int) MCRYPT_DEV_URANDOM);
        if (
            is_string($buf)
                &&
            RandomCompat_strlen($buf) === $bytes
        ) {
            /**
             * Return our random entropy buffer here:
             */
            return $buf;
        }

        /**
         * If we reach here, PHP has failed us.
         */
        throw new Exception(
            'Could not gather sufficient random data'
        );
    }
}