Creating hash in Go

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

Creating hash in Go

问题

免责声明:我对Go语言不熟悉。

我正在尝试将一个Python函数转换为Go语言:

def verify_signature(self, token, timestamp, signature):
    return signature == hmac.new(key=self.api_key,
        msg='{0}{1}'.format(timestamp, token),
        digestmod=hashlib.sha256).hexdigest()

我认为我下面的Go函数接近正确,但是我不确定Python函数的最后一行digestmod=hashlib.sha256).hexdigest()的确切含义:

func verify_signature(api_key, token, timestamp) {
    msg := fmt.Sprintf("%s%s", timestamp, token)
    mac := hmac.New(sha256.New, api_key)
    mac.Write(msg) 
    return mac
}

请注意,这只是我根据我的理解尝试的翻译,可能不完全准确。建议您查阅Go语言相关文档或咨询Go语言专家以获得准确的答案。

英文:

Disclaimer: I am new to Go

I am trying to convert a Python function to Go:

def verify_signature(self, token, timestamp, signature):
    return signature == hmac.new(key=self.api_key,
        msg='{0}{1}'.format(timestamp, token),
        digestmod=hashlib.sha256).hexdigest()

I think my Go function below is close to correct, but I do not understand the last line of the Python function "digestmod=hashlib.sha256).hexdigest()" to say for certain:

func verify_signature (api_key, token, timestamp) {
    msg := fmt.Sprintf("%s%s", timestamp, token)
    mac := hmac.New(sha256.New, api_key)
    mac.Write(msg) 
    return mac
}

答案1

得分: 1

代码在不同的格式下更容易理解:

def verify_signature(self, token, timestamp, signature):
    mac = hmac.new(
        key=self.api_key,
        msg='{}{}'.format(timestamp, token),
        digestmod=hashlib.sha256
    )
    actual = mac.hexdigest()
    return signature == actual

回答你的问题:hexdigest() 将把应用 SHA256 算法后得到的 64 字节数组转换为一个 128 字符的十六进制字符串,这样更容易处理(打印/比较)。

你的 Go 代码没有做同样的事情。它只是“创建 actual”部分。你缺少“与预期的 signature 进行比较”的部分。如果在 Go 中预期的 signature 已经是一个 []byte 数组,你可以使用 hmac.Equal() 来进行比较。

注意:你可以使用 mac.Sum(nil) 来获取哈希值。参考 https://golang.org/src/crypto/hmac/hmac.go

如果不是 []byte 数组,那么你应该尝试将 signature 中的十六进制字符串转换为 []byte 数组,这样你就可以进行比较了。

英文:

The code is a bit easier to understand when you format it differently:

def verify_signature(self, token, timestamp, signature):
    mac = hmac.new(
        key=self.api_key,
        msg='{0}{1}'.format(timestamp, token),
        digestmod=hashlib.sha256
    )
    actual = mac.hexdigest()
    return signature == actual

To answer your question: hexdigest() will convert the 64 byte array (= the result of applying SHA256 to the message) into a 128 character hex dump which is easier to handle (print/compare).

Your Go code doesn't do the same thing. It's just the "create actual" part. You're missing the "compare with expected signature" part. If the expected signature is already a []byte array in Go, you can use hmac.Equal() to check them.

Note: You get the value of the hash with mac.Sum(nil). See https://golang.org/src/crypto/hmac/hmac.go

If it's not, then you should try to convert the hex dump in signature into a []byte array so you can compare the two.

huangapple
  • 本文由 发表于 2015年9月26日 17:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/32795811.html
匿名

发表评论

匿名网友

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

确定