将 python uuid 哈希码转换为 php

Convert python uuid hashing code to php

我正在尝试将大块 python 代码移植到 php - 在这样做的同时我发现了这一行我找不到等效的代码:

    instance = resp.get(
        'instance',
        uuid.uuid4().hex.encode('utf-8')
    )

    sha = hashlib.sha1(
        self.username.encode('utf-8') + instance
    )
    self.params.update({'id': sha.hexdigest().upper()})

我发现 https://github.com/ramsey/uuid 可以生成 uuid4 并尝试像这样模仿上面的内容:

    if (isset($resp['instance']))
    {
        $instance = $resp['instance'];
    }
    else
    {
        $uuid4 = Uuid::uuid4();
        $instance = utf8_encode(dechex($uuid4->toString()));
    }

    $sha = sha1(utf8_encode($this->username) . $instance);

    $this->params['id'] = strtoupper($sha);

这似乎没有提供相同的结果。任何人都可以帮助我在 php 中产生与 python 中相同的结果。谢谢。

事实证明 dechex 函数与 python 的 hex 不同。 在这种情况下,十六进制函数的结果是从 uuid 中删除破折号。用 str_replace.

解决了