2022-06-01 22:07:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-09 03:19:55 -08:00
|
|
|
* Secure random token generation.
|
2022-06-01 22:07:28 +02:00
|
|
|
*
|
|
|
|
|
* @package NamelessMC\Core
|
|
|
|
|
* @author Derkades
|
|
|
|
|
* @version 2.0.0-pr13
|
|
|
|
|
* @license MIT
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
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.
|
2024-03-09 03:19:55 -08:00
|
|
|
* @return string Generated string.
|
2022-06-01 22:07:28 +02:00
|
|
|
*/
|
2024-03-09 03:19:55 -08: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)));
|
|
|
|
|
}
|
|
|
|
|
}
|