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

StackOverflow https://stackoverflow.com/questions/18262088

  •  24-06-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top