HMAC在Python3与Golang中产生不同的字符串

发布于 2025-01-19 19:47:41 字数 1321 浏览 3 评论 0原文

我有一个Golang功能,可以为给定的键和挑战计算HMAC。 GO代码是

func get_hmac64(psk string, challenge string) string {
    mac := hmac.New(sha3.New256, []byte(psk))
    mac.Write([]byte(challenge))
    macSum := mac.Sum(nil)
    var macSumB64 = make([]byte, (len(macSum)*8+5)/6)
    base64.URLEncoding.Encode(macSumB64, macSum)
    return string(macSumB64)
}

我将其转换为python3如下。

    def get_hmac64(self, psk: str, challenge: str) ->str:
        """
        This generates a different string for same psk and challenge between Golang and Python3
        """
        psk_bytes = bytes(psk, "utf-8")
        challenge_bytes = bytes(challenge, "utf-8")
        mac = hmac.new(psk_bytes, challenge_bytes, hashlib.sha3_256)
        mac_digest = mac.digest()
        b64_encoded = base64.b64decode(mac_digest)
        return b64_encoded.decode("utf-8")
  
    print(get_hmac("abc", "def"))

For the python implementation the string returned is M1P63mj5ytdYUaJJ4m2UMtEKBgRG/K3AzHCW/TjIS1k= whereas for the Go code the generated input for the same key and string is qWISO-QliNl_dwhDBhkd3MaT< /code>

如果挑战hashHMAC实现中保持不变,则值不应该相同。跨语言?如果是这样,我在Python翻译中错过的步骤是什么?

I have a Golang function that calculates hmac for a given key and challenge. The go code is

func get_hmac64(psk string, challenge string) string {
    mac := hmac.New(sha3.New256, []byte(psk))
    mac.Write([]byte(challenge))
    macSum := mac.Sum(nil)
    var macSumB64 = make([]byte, (len(macSum)*8+5)/6)
    base64.URLEncoding.Encode(macSumB64, macSum)
    return string(macSumB64)
}

I translated this to Python3 as below.

    def get_hmac64(self, psk: str, challenge: str) ->str:
        """
        This generates a different string for same psk and challenge between Golang and Python3
        """
        psk_bytes = bytes(psk, "utf-8")
        challenge_bytes = bytes(challenge, "utf-8")
        mac = hmac.new(psk_bytes, challenge_bytes, hashlib.sha3_256)
        mac_digest = mac.digest()
        b64_encoded = base64.b64decode(mac_digest)
        return b64_encoded.decode("utf-8")
  
    print(get_hmac("abc", "def"))

For the python implementation the string returned is M1P63mj5ytdYUaJJ4m2UMtEKBgRG/K3AzHCW/TjIS1k= whereas for the Go code the generated input for the same key and string is qWISO-QliNl_dwhDBhkd3MaT

Shouldnt the value be same if the key, challenge and the hash remains same for the hmac implementation across languages? If so, what is the step that I missed in the Python translation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2025-01-26 19:47:41

以下代码给出的结果与您针对 Python 代码报告的结果相同:(

func get_hmac64(psk string, challenge string) string {
    mac := hmac.New(sha3.New256, []byte(psk))
    mac.Write([]byte(challenge))
    macSum := mac.Sum(nil)
    return base64.StdEncoding.EncodeToString(macSum)
}

完整代码位于 https ://go.dev/play/p/5lWG-jMfELz)。
两个区别是我使用了 sha3.New256 而不是奇怪的 NewDragonHash ,并且我通过简单地使用 .EncodeToString() 修复了 base64 编码代码>方法。

The following code gives the same results as the one you report for your Python code:

func get_hmac64(psk string, challenge string) string {
    mac := hmac.New(sha3.New256, []byte(psk))
    mac.Write([]byte(challenge))
    macSum := mac.Sum(nil)
    return base64.StdEncoding.EncodeToString(macSum)
}

(Full code at https://go.dev/play/p/5lWG-jMfELz).
The two differences are that I used sha3.New256 instead of the strange NewDragonHash, and that I fixed the base64 encoding by simply using the .EncodeToString() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文