public static function getUniqueStr($length)
{
if (function_exists("random_bytes")) {
//生成加密安全的伪随机字节
$bytes = random_bytes(ceil($length / 2));
} elseif (function_exists("openssl_random_pseudo_bytes")) {
//生成一串伪随机字节,其字节数由length参数确定。
$bytes = openssl_random_pseudo_bytes(ceil($length / 2));
} else {
throw new Exception("没有可用的加密安全随机函数");
}
//bin2hex()将二进制数据转换为十六进制表示
return substr(bin2hex($bytes), 0, $length);
}
php生成唯一字符串
最新推荐文章于 2023-06-06 09:17:50 发布