function ope_crypto_rand_secure($min, $max)
{
$range = $max - $min;
if ($range < 0) {
return $min;
}
// not so random...
$log = log($range, 2);
$bytes = (int) ($log / 8) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd&$filter; // discard irrelevant bits
} while ($rnd >= $range);
return $min + $rnd;
}
function ope_getToken($length)
{
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet .= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet .= "0123456789";
$codeAlphabet .= "[]_-()|,~.^?/$&`{};+@#";
for ($i = 0; $i < $length; $i++) {
$token .= $codeAlphabet[ope_crypto_rand_secure(0, strlen($codeAlphabet))];
}
return $token;
}
$ope_token = ope_getToken(64); // 生成64位随机字符串
评论区
发表新的留言
您可以留言提出您的疑问或建议。
您的留言得到回复时,会通过您填写的邮箱提醒您。