diff options
Diffstat (limited to 'includes/util/random.inc')
-rw-r--r-- | includes/util/random.inc | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/includes/util/random.inc b/includes/util/random.inc new file mode 100644 index 0000000..b6e7905 --- /dev/null +++ b/includes/util/random.inc @@ -0,0 +1,15 @@ +<?php + +/** + * @throws Exception + */ +function random($length = 13): string { + if (function_exists("random_bytes")) { + $bytes = random_bytes(ceil($length / 2)); + } elseif (function_exists("openssl_random_pseudo_bytes")) { + $bytes = openssl_random_pseudo_bytes(ceil($length / 2)); + } else { + throw new Exception("No cryptographically secure random function available"); + } + return substr(bin2hex($bytes), 0, $length); +}
\ No newline at end of file |