WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/08/2017 11:47:04 AM (4 years ago)
Author:
dd32
Message:

External Libraries: Update Random_Compat from 1.2.1 to 2.0.11.

Notably this fixes PHP7 parse errors of the files and removes the OpenSSL functionality.
Full Changes: https://github.com/paragonie/random_compat/compare/v1.2.1...v2.0.11

Props jrdelarosa.
See #42439.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/random_compat/random_bytes_libsodium_legacy.php

    r36886 r42130  
    55 *
    66 * The MIT License (MIT)
    7  * 
    8  * Copyright (c) 2015 Paragon Initiative Enterprises
     7 *
     8 * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
    99 *
    1010 * Permission is hereby granted, free of charge, to any person obtaining a copy
     
    2727 */
    2828
    29 /**
    30  * If the libsodium PHP extension is loaded, we'll use it above any other
    31  * solution.
    32  *
    33  * libsodium-php project:
    34  * @ref https://github.com/jedisct1/libsodium-php
    35  *
    36  * @param int $bytes
    37  *
    38  * @throws Exception
    39  *
    40  * @return string
    41  */
    42 function random_bytes($bytes)
    43 {
    44     try {
    45         $bytes = RandomCompat_intval($bytes);
    46     } catch (TypeError $ex) {
    47         throw new TypeError(
    48             'random_bytes(): $bytes must be an integer'
     29if (!is_callable('random_bytes')) {
     30    /**
     31     * If the libsodium PHP extension is loaded, we'll use it above any other
     32     * solution.
     33     *
     34     * libsodium-php project:
     35     * @ref https://github.com/jedisct1/libsodium-php
     36     *
     37     * @param int $bytes
     38     *
     39     * @throws Exception
     40     *
     41     * @return string
     42     */
     43    function random_bytes($bytes)
     44    {
     45        try {
     46            $bytes = RandomCompat_intval($bytes);
     47        } catch (TypeError $ex) {
     48            throw new TypeError(
     49                'random_bytes(): $bytes must be an integer'
     50            );
     51        }
     52
     53        if ($bytes < 1) {
     54            throw new Error(
     55                'Length must be greater than 0'
     56            );
     57        }
     58
     59        /**
     60         * @var string
     61         */
     62        $buf = '';
     63
     64        /**
     65         * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
     66         * generated in one invocation.
     67         */
     68        if ($bytes > 2147483647) {
     69            for ($i = 0; $i < $bytes; $i += 1073741824) {
     70                $n = ($bytes - $i) > 1073741824
     71                    ? 1073741824
     72                    : $bytes - $i;
     73                $buf .= Sodium::randombytes_buf((int) $n);
     74            }
     75        } else {
     76            $buf .= Sodium::randombytes_buf((int) $bytes);
     77        }
     78
     79        if (is_string($buf)) {
     80            if (RandomCompat_strlen($buf) === $bytes) {
     81                return $buf;
     82            }
     83        }
     84
     85        /**
     86         * If we reach here, PHP has failed us.
     87         */
     88        throw new Exception(
     89            'Could not gather sufficient random data'
    4990        );
    5091    }
    51 
    52     if ($bytes < 1) {
    53         throw new Error(
    54             'Length must be greater than 0'
    55         );
    56     }
    57 
    58     /**
    59      * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
    60      * generated in one invocation.
    61      */
    62     if ($bytes > 2147483647) {
    63         $buf = '';
    64         for ($i = 0; $i < $bytes; $i += 1073741824) {
    65             $n = ($bytes - $i) > 1073741824
    66                 ? 1073741824
    67                 : $bytes - $i;
    68             $buf .= Sodium::randombytes_buf($n);
    69         }
    70     } else {
    71         $buf = Sodium::randombytes_buf($bytes);
    72     }
    73 
    74     if ($buf !== false) {
    75         if (RandomCompat_strlen($buf) === $bytes) {
    76             return $buf;
    77         }
    78     }
    79 
    80     /**
    81      * If we reach here, PHP has failed us.
    82      */
    83     throw new Exception(
    84         'Could not gather sufficient random data'
    85     );
    8692}
Note: See TracChangeset for help on using the changeset viewer.