mc-cms-namelessmc/core/classes/Core/SecureRandom.php

24 lines
528 B
PHP
Raw Permalink Normal View History

2022-06-01 22:07:28 +02:00
<?php
/**
* Secure random token generation.
2022-06-01 22:07:28 +02:00
*
* @package NamelessMC\Core
* @author Derkades
* @version 2.0.0-pr13
* @license MIT
*/
class SecureRandom
{
2022-06-01 22:07:28 +02:00
/**
* Generate a unique alphanumeric string.
*
* @throws Exception If an appropriate source of randomness cannot be found.
* @return string Generated string.
2022-06-01 22:07:28 +02:00
*/
public static function alphanumeric(int $num_bytes = 32): string
{
2022-06-01 22:07:28 +02:00
return preg_replace('/(\+|\/|\=)*/', '', base64_encode(random_bytes($num_bytes)));
}
}