File "random_bytes_libsodium_legacy.php"

Full path: /home/kosmetik/public_html/wp-includes/random_compat/random_bytes_libsodium_legacy.php
File size: 931 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

if (!is_callable('random_bytes')) {
    function random_bytes($bytes)
    {
        try {
            $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');
        }
        $buf = '';
        if ($bytes > 2147483647) {
            for ($i = 0; $i < $bytes; $i += 1073741824) {
                $n = $bytes - $i > 1073741824 ? 1073741824 : $bytes - $i;
                $buf .= Sodium::randombytes_buf((int) $n);
            }
        } else {
            $buf .= Sodium::randombytes_buf((int) $bytes);
        }
        if (is_string($buf)) {
            if (RandomCompat_strlen($buf) === $bytes) {
                return $buf;
            }
        }
        throw new Exception('Could not gather sufficient random data');
    }
}