Go

import (
  "time"
  "fmt"
  "crypto/hmac"
  "crypto/sha1"
  "encoding/base64"
  "net/url"
  "os"
)

func main() {
  endpoint := os.Args[1]
  method := os.Args[2]

  theUrl, _ := url.Parse(endpoint)

  keyId := "[your key]"
  secret64 := "[your secret]"
  secret := []byte(secret64)

  now := time.Now().UTC()
  formatted := now.Format("Mon, 02 Jan 2006 15:04:05 MST")
  dateHeader := " -H \"Date: " + formatted + "\""

  mac := hmac.New(sha1.New, secret)
  mac.Write([]byte("date: " + formatted + "\n(request-target): " + strings.ToLower(method) + " " + theUrl.Path))
  macBytes := mac.Sum(nil)

  macEncoded := url.QueryEscape(base64.StdEncoding.EncodeToString(macBytes))
  signatureHeader := " -H 'Authorization: Signature keyId=\"" + keyId +
     "\",headers=\"date (request-target)\",algorithm=\"hmac-sha1\",signature=\"" + macEncoded + "\"'"

  if method == "POST" {
      fmt.Println("curl -n -X " + method + " -d @json.txt " + dateHeader + " " + signatureHeader +
      " " + endpoint + service)
  } else {
      fmt.Println("curl -n " + dateHeader + " " + signatureHeader + developerIdHeader +
      " " + endpoint + service)
  }
}

Last updated