我正在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("PROBLEM IN DECODING HUH!")
return false
}
return hmac.Equal(expectedMAC,signatureMAC)}
当我发出go run CSServer时,我会得到这个错误
/CSServer.go:54: undefined: hmac.Equal
为什么?怎么一回事?为什么hmac.New很好,而hmac.Equals却不行呢?
发布于 2013-08-16 00:44:16
不知道问题出在哪里,
但是,在删除代码并将其放入play.golang.org中并看到它在bun上运行良好之后,我检查了它的版本,并查看了我的版本,go1.0.3 --我安装了最新的go1.1.2 darwin/amd64 --解决了非常奇怪的问题。
发布于 2013-08-15 21:35:08
请张贴最低限度,但完整的程序时,要求。没有这一点,我唯一能提供的就是一个编译w/o麻烦的例子。未定义的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("PROBLEM IN DECODING HUH!")
return false
}
return hmac.Equal(expectedMAC, signatureMAC)
}
func main() {}游乐场
https://stackoverflow.com/questions/18262088
复制相似问题