Skip to content

Instantly share code, notes, and snippets.

@eiri
Created November 3, 2017 15:59
Show Gist options
  • Save eiri/5145da6c074c18315bea9d521fe2aef7 to your computer and use it in GitHub Desktop.
Save eiri/5145da6c074c18315bea9d521fe2aef7 to your computer and use it in GitHub Desktop.
Generate hmac256 from a string and present it in hex.
package main
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"fmt"
)
func GenerateSecret() string {
b := make([]byte, 16)
rand.Read(b)
return fmt.Sprintf("%x", b)
}
func ComputeHmacHex(message, secret string) string {
key := []byte(secret)
h := hmac.New(sha1.New, key)
h.Write([]byte(message))
return hex.EncodeToString(h.Sum(nil))
}
func main() {
secret := GenerateSecret()
key := "system.load15"
idx := ComputeHmacHex(key, secret)
fmt.Println("secret:", secret)
fmt.Println("key:", key)
fmt.Println("idx:", idx)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment