From f51ee90c66527fd7ff634f3e8d414cb670da068d Mon Sep 17 00:00:00 2001 From: plegall Date: Tue, 26 Apr 2016 11:07:44 +0200 Subject: bug #470, use a dedicated lib to generate random bytes --- include/random_compat/random_bytes_libsodium.php | 86 ++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 include/random_compat/random_bytes_libsodium.php (limited to 'include/random_compat/random_bytes_libsodium.php') diff --git a/include/random_compat/random_bytes_libsodium.php b/include/random_compat/random_bytes_libsodium.php new file mode 100644 index 000000000..f802d4e12 --- /dev/null +++ b/include/random_compat/random_bytes_libsodium.php @@ -0,0 +1,86 @@ + 2147483647) { + $buf = ''; + for ($i = 0; $i < $bytes; $i += 1073741824) { + $n = ($bytes - $i) > 1073741824 + ? 1073741824 + : $bytes - $i; + $buf .= \Sodium\randombytes_buf($n); + } + } else { + $buf = \Sodium\randombytes_buf($bytes); + } + + if ($buf !== false) { + if (RandomCompat_strlen($buf) === $bytes) { + return $buf; + } + } + + /** + * If we reach here, PHP has failed us. + */ + throw new Exception( + 'Could not gather sufficient random data' + ); +} -- cgit v1.2.3