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

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

"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() {}

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.

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() {}

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:

确定