How to generate HMAC

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

How to generate HMAC

问题

我需要在Go中创建一个Hmac。我已经在nodejs中创建了一个Hmac,现在需要在Go中生成相同的Hmac。我尝试了以下代码,但输出完全不同。我不知道我做错了什么。

这是我尝试的代码:

  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "fmt"
  7. )
  8. func ComputeHmac256(message string, secret string) string {
  9. key := []byte(secret)
  10. h := hmac.New(sha256.New, key)
  11. h.Write([]byte(message))
  12. return base64.StdEncoding.EncodeToString(h.Sum(nil))
  13. }
  14. func main() {
  15. fmt.Println(ComputeHmac256("sms1", "b5fb5b3a65b8429693c3a029308e2e46"))
  16. }

输出:JVN7kUPFL0aQ09lIH4YOsFJA3A2faqTuu6zIaYo61VI=

需要Go中等效的以下nodejs代码

  1. var crypto = require('crypto'),
  2. text = 'sms1',
  3. key = 'b5fb5b3a65b8429693c3a029308e2e46';
  4. var hash = crypto.createHmac('sha256', key)
  5. hash.update(text)
  6. var value = hash.digest('hex')
  7. // 输出
  8. 25537b9143c52f4690d3d9481f860eb05240dc0d9f6aa4eebbacc8698a3ad552

请注意,这只是代码的翻译,我无法提供关于代码是否正确的任何保证。

英文:

I need to create an Hmac in Go. I have created an Hmac in nodejs, need to generate the same Hamc in Go. Tried following code but getting exactly different output. I don't know what I am doing wrong.
This is what I tried

  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "fmt"
  7. )
  8. func ComputeHmac256(message string, secret string) string {
  9. key := []byte(secret)
  10. h := hmac.New(sha256.New, key)
  11. h.Write([]byte(message))
  12. return base64.StdEncoding.EncodeToString(h.Sum(nil))
  13. }
  14. func main() {
  15. fmt.Println(ComputeHmac256("sms1", "b5fb5b3a65b8429693c3a029308e2e46"))
  16. }
  17. Output: JVN7kUPFL0aQ09lIH4YOsFJA3A2faqTuu6zIaYo61VI=

Need go equivalent of following nodejs code

  1. var crypto = require('crypto'),
  2. text = 'sms1',
  3. key = 'b5fb5b3a65b8429693c3a029308e2e46'
  4. var hash = crypto.createHmac('sha256', key)
  5. hash.update(text)
  6. var value = hash.digest('hex')
  7. // Output
  8. 25537b9143c52f4690d3d9481f860eb05240dc0d9f6aa4eebbacc8698a3ad552

答案1

得分: 14

你需要在Go程序中使用与Node.js程序相同的编码方式(十六进制):

  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "fmt"
  7. )
  8. func ComputeHmac256(message string, secret string) string {
  9. key := []byte(secret)
  10. h := hmac.New(sha256.New, key)
  11. h.Write([]byte(message))
  12. return hex.EncodeToString(h.Sum(nil))
  13. }
  14. func main() {
  15. fmt.Println(ComputeHmac256("sms1", "b5fb5b3a65b8429693c3a029308e2e46"))
  16. }

你可以在这里查看代码:https://play.golang.org/p/-1yePFeipT

英文:

You need to use the same encoding in your Go program as you do in your Node.js program (hex):

  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "fmt"
  7. )
  8. func ComputeHmac256(message string, secret string) string {
  9. key := []byte(secret)
  10. h := hmac.New(sha256.New, key)
  11. h.Write([]byte(message))
  12. return hex.EncodeToString(h.Sum(nil))
  13. }
  14. func main() {
  15. fmt.Println(ComputeHmac256("sms1", "b5fb5b3a65b8429693c3a029308e2e46"))
  16. }

https://play.golang.org/p/-1yePFeipT

huangapple
  • 本文由 发表于 2016年9月10日 20:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/39426096.html
匿名

发表评论

匿名网友

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

确定