mirror of
https://github.com/delight-im/PHP-Auth.git
synced 2025-07-11 03:26:20 +02:00
Move method 'createRandomString' from class 'Auth' to 'UserManager'
This commit is contained in:
18
src/Auth.php
18
src/Auth.php
@ -1397,24 +1397,6 @@ final class Auth extends UserManager {
|
|||||||
return $params;
|
return $params;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a random string with the given maximum length
|
|
||||||
*
|
|
||||||
* With the default parameter, the output should contain at least as much randomness as a UUID
|
|
||||||
*
|
|
||||||
* @param int $maxLength the maximum length of the output string (integer multiple of 4)
|
|
||||||
* @return string the new random string
|
|
||||||
*/
|
|
||||||
public static function createRandomString($maxLength = 24) {
|
|
||||||
// calculate how many bytes of randomness we need for the specified string length
|
|
||||||
$bytes = floor(intval($maxLength) / 4) * 3;
|
|
||||||
// get random data
|
|
||||||
$data = openssl_random_pseudo_bytes($bytes);
|
|
||||||
|
|
||||||
// return the Base64-encoded result
|
|
||||||
return Base64::encode($data, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a UUID v4 as per RFC 4122
|
* Creates a UUID v4 as per RFC 4122
|
||||||
*
|
*
|
||||||
|
@ -27,6 +27,24 @@ abstract class UserManager {
|
|||||||
/** @var PdoDatabase the database connection to operate on */
|
/** @var PdoDatabase the database connection to operate on */
|
||||||
protected $db;
|
protected $db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a random string with the given maximum length
|
||||||
|
*
|
||||||
|
* With the default parameter, the output should contain at least as much randomness as a UUID
|
||||||
|
*
|
||||||
|
* @param int $maxLength the maximum length of the output string (integer multiple of 4)
|
||||||
|
* @return string the new random string
|
||||||
|
*/
|
||||||
|
public static function createRandomString($maxLength = 24) {
|
||||||
|
// calculate how many bytes of randomness we need for the specified string length
|
||||||
|
$bytes = floor(intval($maxLength) / 4) * 3;
|
||||||
|
// get random data
|
||||||
|
$data = openssl_random_pseudo_bytes($bytes);
|
||||||
|
|
||||||
|
// return the Base64-encoded result
|
||||||
|
return Base64::encode($data, true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param PdoDatabase|PdoDsn|\PDO $databaseConnection the database connection to operate on
|
* @param PdoDatabase|PdoDsn|\PDO $databaseConnection the database connection to operate on
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user