Created
September 24, 2014 15:28
-
-
Save turret-io/42f9c2f4e58b51fcfe99 to your computer and use it in GitHub Desktop.
Generate HMAC in Go
This file contains 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/sha512" | |
"encoding/base64" | |
"encoding/json" | |
"fmt" | |
"strconv" | |
"time" | |
) | |
const sharedSecret = "sup3rs3cr3t!!" | |
func signString(stringToSign []byte, sharedSecret []byte) []byte { | |
h := hmac.New(sha512.New, sharedSecret) | |
h.Write(stringToSign) | |
return h.Sum(nil) | |
} | |
func main() { | |
payload := make(map[string]string) | |
payload["name"] = "joe smith" | |
payload["category"] = "people" | |
payload["action"] = "transport" | |
payload["where"] = "pluto" | |
payload["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10) | |
jsonBytes, err := json.Marshal(payload) | |
if err != nil { | |
panic(err) | |
} | |
signature := signString(jsonBytes, []byte(sharedSecret)) | |
fmt.Println(fmt.Sprintf("/?data=%s&signature=%s", | |
base64.URLEncoding.EncodeToString(jsonBytes), | |
base64.URLEncoding.EncodeToString(signature))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment