在这行代码之前,hmac.New正常工作,但出现了”undefined: hmac.Equal”错误。

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

"undefined: hmac.Equal" error while hmac.New in the line before this works fine

问题

我正在开发一个使用Go语言的Web服务器,代码如下:

  1. import (
  2. "net/http"
  3. "log"
  4. "fmt"
  5. "encoding/json"
  6. "encoding/hex"
  7. "time"
  8. "math/rand"
  9. "crypto/sha256"
  10. "crypto/hmac"
  11. "strconv"
  12. "strings"
  13. "github.com/crowdmob/goamz/aws"
  14. "github.com/crowdmob/goamz/dynamodb"
  15. )
  16. func singSomething(someid string) string {
  17. mac := hmac.New(sha256.New, key)
  18. mac.Write([]byte(id))
  19. b := mac.Sum(nil)
  20. return hex.EncodeToString(b)
  21. }
  22. func validateSignature(id, signature string) bool {
  23. mac := hmac.New(sha256.New, key)
  24. mac.Write([]byte(id))
  25. expectedMAC := mac.Sum(nil)
  26. signatureMAC, err := hex.DecodeString(signature)
  27. if err != nil {
  28. fmt.Println("解码出错!")
  29. return false
  30. }
  31. return hmac.Equal(expectedMAC, signatureMAC)
  32. }

当我运行go run CSServer时,出现了以下错误:/CSServer.go:54: undefined: hmac.Equal

为什么会出现这个错误?为什么hmac.New没有问题,但hmac.Equal有问题?

英文:

I am developing a web server in go,
at the top I have

  1. import ("net/http"
  2. "log"
  3. "fmt"
  4. "encoding/json"
  5. "encoding/hex"
  6. "time"
  7. "math/rand"
  8. "crypto/sha256"
  9. "crypto/hmac"
  10. "strconv"
  11. "strings"
  12. "github.com/crowdmob/goamz/aws"
  13. "github.com/crowdmob/goamz/dynamodb"
  14. )

later I have

  1. func singSomething(someid string) string {
  2. mac := hmac.New(sha256.New, key)
  3. mac.Write([]byte(id))
  4. b := mac.Sum(nil)
  5. return hex.EncodeToString(b)
  6. }
  7. func validateSignature(id, signature string) bool {
  8. mac := hmac.New(sha256.New, key)
  9. mac.Write([]byte(id))
  10. expectedMAC := mac.Sum(nil)
  11. signatureMAC, err := hex.DecodeString(signature)
  12. if err != nil {
  13. fmt.Println("PROBLEM IN DECODING HUH!")
  14. return false
  15. }
  16. return hmac.Equal(expectedMAC,signatureMAC)

}

I get this error when I issue go run CSServer
/CSServer.go:54: undefined: hmac.Equal

Why? What is going on? How come hmac.New is fine but hmac.Equals is not?

答案1

得分: 0

请在提问中提供尽可能完整的代码示例。如果没有提供完整的代码示例,我只能提供一个可以编译的示例,即未定义的hmac.Equal不会显示问题。你没有展示的代码中可能存在其他问题。

  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "fmt"
  7. )
  8. func singSomething(someid string) string {
  9. mac := hmac.New(sha256.New, []byte{})
  10. mac.Write([]byte(someid))
  11. b := mac.Sum(nil)
  12. return hex.EncodeToString(b)
  13. }
  14. func validateSignature(id, signature string) bool {
  15. mac := hmac.New(sha256.New, []byte{})
  16. mac.Write([]byte(id))
  17. expectedMAC := mac.Sum(nil)
  18. signatureMAC, err := hex.DecodeString(signature)
  19. if err != nil {
  20. fmt.Println("解码出现问题!")
  21. return false
  22. }
  23. return hmac.Equal(expectedMAC, signatureMAC)
  24. }
  25. func main() {}

Playground

英文:

Please post minimal, but complete programs when asking. Without that, the only thing I can provide is an example which compiles w/o trouble, ie. the undefined hmac.Equal doesn't demonstrate. There must be some problem elsewhere in the code you didn't show.

  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "fmt"
  7. )
  8. func singSomething(someid string) string {
  9. mac := hmac.New(sha256.New, []byte{})
  10. mac.Write([]byte(someid))
  11. b := mac.Sum(nil)
  12. return hex.EncodeToString(b)
  13. }
  14. func validateSignature(id, signature string) bool {
  15. mac := hmac.New(sha256.New, []byte{})
  16. mac.Write([]byte(id))
  17. expectedMAC := mac.Sum(nil)
  18. signatureMAC, err := hex.DecodeString(signature)
  19. if err != nil {
  20. fmt.Println("PROBLEM IN DECODING HUH!")
  21. return false
  22. }
  23. return hmac.Equal(expectedMAC, signatureMAC)
  24. }
  25. func main() {}

Playground

答案2

得分: 0

不知道问题出在哪里,但是在将代码精简并放入play.golang.org进行测试后,发现它在那里可以正常工作,但在我的机器上却不行。我检查了我的版本,发现是go1.0.3,然后我安装了最新的go1.1.2 darwin/amd64版本,问题解决了,非常奇怪。

英文:

Don't know what the problem was,
but after striping down the code and puting it in play.golang.org and seeing that it works fine there bun not on my machine, I checked and the my version, it was go1.0.3 I installed the latest go1.1.2 darwin/amd64 and problem solved, very weird.

huangapple
  • 本文由 发表于 2013年8月16日 05:21:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/18262088.html
匿名

发表评论

匿名网友

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

确定