英文:
"undefined: hmac.Equal" error while hmac.New in the line before this works fine
问题
我正在开发一个使用Go语言的Web服务器,代码如下:
import (
"net/http"
"log"
"fmt"
"encoding/json"
"encoding/hex"
"time"
"math/rand"
"crypto/sha256"
"crypto/hmac"
"strconv"
"strings"
"github.com/crowdmob/goamz/aws"
"github.com/crowdmob/goamz/dynamodb"
)
func singSomething(someid string) string {
mac := hmac.New(sha256.New, key)
mac.Write([]byte(id))
b := mac.Sum(nil)
return hex.EncodeToString(b)
}
func validateSignature(id, signature string) bool {
mac := hmac.New(sha256.New, key)
mac.Write([]byte(id))
expectedMAC := mac.Sum(nil)
signatureMAC, err := hex.DecodeString(signature)
if err != nil {
fmt.Println("解码出错!")
return false
}
return hmac.Equal(expectedMAC, signatureMAC)
}
当我运行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
import ("net/http"
"log"
"fmt"
"encoding/json"
"encoding/hex"
"time"
"math/rand"
"crypto/sha256"
"crypto/hmac"
"strconv"
"strings"
"github.com/crowdmob/goamz/aws"
"github.com/crowdmob/goamz/dynamodb"
)
later I have
func singSomething(someid string) string {
mac := hmac.New(sha256.New, key)
mac.Write([]byte(id))
b := mac.Sum(nil)
return hex.EncodeToString(b)
}
func validateSignature(id, signature string) bool {
mac := hmac.New(sha256.New, key)
mac.Write([]byte(id))
expectedMAC := mac.Sum(nil)
signatureMAC, err := hex.DecodeString(signature)
if err != nil {
fmt.Println("PROBLEM IN DECODING HUH!")
return false
}
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
不会显示问题。你没有展示的代码中可能存在其他问题。
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
)
func singSomething(someid string) string {
mac := hmac.New(sha256.New, []byte{})
mac.Write([]byte(someid))
b := mac.Sum(nil)
return hex.EncodeToString(b)
}
func validateSignature(id, signature string) bool {
mac := hmac.New(sha256.New, []byte{})
mac.Write([]byte(id))
expectedMAC := mac.Sum(nil)
signatureMAC, err := hex.DecodeString(signature)
if err != nil {
fmt.Println("解码出现问题!")
return false
}
return hmac.Equal(expectedMAC, signatureMAC)
}
func main() {}
英文:
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.
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
)
func singSomething(someid string) string {
mac := hmac.New(sha256.New, []byte{})
mac.Write([]byte(someid))
b := mac.Sum(nil)
return hex.EncodeToString(b)
}
func validateSignature(id, signature string) bool {
mac := hmac.New(sha256.New, []byte{})
mac.Write([]byte(id))
expectedMAC := mac.Sum(nil)
signatureMAC, err := hex.DecodeString(signature)
if err != nil {
fmt.Println("PROBLEM IN DECODING HUH!")
return false
}
return hmac.Equal(expectedMAC, signatureMAC)
}
func main() {}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论