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

huangapple go评论80阅读模式
英文:

HMAC produces different strings in Python3 vs Golang

问题

我有一个用于计算给定密钥和挑战的hmac的Golang函数。Golang代码如下:

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(psk: str, challenge: str) -> str:
    """
    这会在Golang和Python3之间为相同的psk和challenge生成不同的字符串
    """
    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.b64encode(mac_digest)
    return b64_encoded.decode("utf-8")

print(get_hmac64("abc", "def"))

对于Python实现,返回的字符串是M1P63mj5ytdYUaJJ4m2UMtEKBgRG/K3AzHCW/TjIS1k=,而对于Go代码,相同密钥和字符串的生成输入是qWISO-QliNl_dwhDBhkd3MaT

如果keychallengehash在不同语言的hmac实现中保持不变,那么返回值不应该相同吗?如果是这样,那么我在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?

答案1

得分: 1

以下代码与您报告的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.

huangapple
  • 本文由 发表于 2022年4月6日 20:28:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/71766757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定