Created
November 3, 2017 15:59
-
-
Save eiri/5145da6c074c18315bea9d521fe2aef7 to your computer and use it in GitHub Desktop.
Generate hmac256 from a string and present it in hex.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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