str_random()函数是否像uniqid()函数一样使用任何时间戳?

问题描述 投票:0回答:1

我正在为移动应用程序开发API,我想为用户验证创建唯一的令牌。目前,我正在为此使用str_random(30)函数。

基本上我想知道str_random()函数的工作方式。它使用任何时间戳吗?

public function generateToken($user_id)
    {
        $randToken = str_random(30);
        if (Token::updateOrCreate([
            'user_id' => $user_id,
        ], [
            'user_id' => $user_id,
            "token"   => $randToken
        ])
        ) {
            return $randToken;
        }

        return "";
    }
php laravel octobercms
1个回答
0
投票
这是github上用于随机字符串生成的确切代码:https://github.com/laravel/framework/blob/5.8/src/Illuminate/Support/Str.php#L323

/** * Generate a more truly "random" alpha-numeric string. * * @param int $length * @return string */ public static function random($length = 16) { $string = ''; while (($len = strlen($string)) < $length) { $size = $length - $len; $bytes = random_bytes($size); $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size); } return $string; }

据我所知,它不使用任何时间戳来生成随机字符串

© www.soinside.com 2019 - 2024. All rights reserved.