现在的态度决定你未来的高度

Oauth2 AccessToken生成规则

示例

function generateAccessToken()
{
    if (function_exists('random_bytes')) {
        $randomData = random_bytes(20);
        if ($randomData !== false && strlen($randomData) === 20) {
            return bin2hex($randomData);
        }
    }
    if (function_exists('openssl_random_pseudo_bytes')) {
        $randomData = openssl_random_pseudo_bytes(20);
        if ($randomData !== false && strlen($randomData) === 20) {
            return bin2hex($randomData);
        }
    }
    if (function_exists('mcrypt_create_iv')) {
        $randomData = mcrypt_create_iv(20, MCRYPT_DEV_URANDOM);
        if ($randomData !== false && strlen($randomData) === 20) {
            return bin2hex($randomData);
        }
    }
    if (@file_exists('/dev/urandom')) { // Get 100 bytes of random data
        $randomData = file_get_contents('/dev/urandom', false, null, 0, 20);
        if ($randomData !== false && strlen($randomData) === 20) {
            return bin2hex($randomData);
        }
    }
    // Last resort which you probably should just get rid of:
    $randomData = mt_rand() . mt_rand() . mt_rand() . mt_rand() . microtime(true) . uniqid(mt_rand(), true);
    return substr(hash('sha512', $randomData), 0, 40);
}
发表新评论