summaryrefslogtreecommitdiff
path: root/includes/util/random.inc
blob: b6e7905d8b47e90c6335cfad7b22b125ba0d7e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}