- Timestamp:
- 11/08/2017 11:47:04 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/random_compat/random_bytes_libsodium.php
r36886 r42130 5 5 * 6 6 * The MIT License (MIT) 7 * 8 * Copyright (c) 2015 Paragon Initiative Enterprises7 * 8 * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises 9 9 * 10 10 * Permission is hereby granted, free of charge, to any person obtaining a copy … … 27 27 */ 28 28 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' 29 if (!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 * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be 61 * generated in one invocation. 62 */ 63 if ($bytes > 2147483647) { 64 $buf = ''; 65 for ($i = 0; $i < $bytes; $i += 1073741824) { 66 $n = ($bytes - $i) > 1073741824 67 ? 1073741824 68 : $bytes - $i; 69 $buf .= \Sodium\randombytes_buf($n); 70 } 71 } else { 72 $buf = \Sodium\randombytes_buf($bytes); 73 } 74 75 if ($buf !== false) { 76 if (RandomCompat_strlen($buf) === $bytes) { 77 return $buf; 78 } 79 } 80 81 /** 82 * If we reach here, PHP has failed us. 83 */ 84 throw new Exception( 85 'Could not gather sufficient random data' 49 86 ); 50 87 } 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 be60 * generated in one invocation.61 */62 if ($bytes > 2147483647) {63 $buf = '';64 for ($i = 0; $i < $bytes; $i += 1073741824) {65 $n = ($bytes - $i) > 107374182466 ? 107374182467 : $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 );86 88 }
Note: See TracChangeset
for help on using the changeset viewer.