summaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
authorSimon Rettberg2017-03-02 14:27:00 +0100
committerSimon Rettberg2017-03-02 14:27:00 +0100
commit7b198f93c1848589501ff8e41809412aa7ff72f9 (patch)
tree8e53c4a42d02b629835b2c597921bdbcf45d66ce /inc
parent[systemstatus] Add lighttpd error log (diff)
parent[statistics_reporting] fixed column selector order (diff)
downloadslx-admin-7b198f93c1848589501ff8e41809412aa7ff72f9.tar.gz
slx-admin-7b198f93c1848589501ff8e41809412aa7ff72f9.tar.xz
slx-admin-7b198f93c1848589501ff8e41809412aa7ff72f9.zip
Merge branch 'statistics_reporting'
Diffstat (limited to 'inc')
-rw-r--r--inc/util.inc.php52
1 files changed, 52 insertions, 0 deletions
diff --git a/inc/util.inc.php b/inc/util.inc.php
index 671028ed..d454d18d 100644
--- a/inc/util.inc.php
+++ b/inc/util.inc.php
@@ -365,4 +365,56 @@ SADFACE;
exit(0);
}
+ /**
+ * Return a binary string of given length, containing
+ * random bytes. If $secure is given, only methods of
+ * obtaining cryptographically strong random bytes will
+ * be used, otherwise, weaker methods might be used.
+ *
+ * @param int $length number of bytes to return
+ * @param bool $secure true = only use strong random sources
+ * @return string|bool string of requested length, false on error
+ */
+ public static function randomBytes($length, $secure)
+ {
+ if (function_exists('random_bytes')) {
+ return random_bytes($length);
+ }
+ if ($secure) {
+ if (function_exists('openssl_random_pseudo_bytes')) {
+ $bytes = openssl_random_pseudo_bytes($length, $ok);
+ if ($bytes !== false && $ok) {
+ return $bytes;
+ }
+ }
+ $file = '/dev/random';
+ } else {
+ $file = '/dev/urandom';
+ }
+ $fh = @fopen($file, 'r');
+ if ($fh !== false) {
+ $bytes = fread($fh, $length);
+ while ($bytes !== false && strlen($bytes) < $length) {
+ $new = fread($fh, $length - strlen($bytes));
+ if ($new === false) {
+ $bytes = false;
+ break;
+ }
+ $bytes .= $new;
+ }
+ fclose($fh);
+ if ($bytes !== false) {
+ return $bytes;
+ }
+ }
+ if ($secure) {
+ return false;
+ }
+ $bytes = '';
+ while ($length > 0) {
+ $bytes .= chr(mt_rand(0, 255));
+ }
+ return $bytes;
+ }
+
}