Skip to content

Instantly share code, notes, and snippets.

@adityathebe
Created December 17, 2020 07:27
Show Gist options
  • Save adityathebe/6af426c7b8cd9cdb4486eb1c3a32f4fc to your computer and use it in GitHub Desktop.
Save adityathebe/6af426c7b8cd9cdb4486eb1c3a32f4fc to your computer and use it in GitHub Desktop.
Calculate HS256 with secret keys read from a file
// https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/.
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
)
func main() {
src := []byte("YOUR-MESSAGE")
key, err := ioutil.ReadFile("path-to-secret-key")
if err != nil {
log.Fatal(err)
}
mac := hmac.New(sha256.New, key)
mac.Write(src)
encoded := base64.StdEncoding.EncodeToString(mac.Sum(nil))
fmt.Print(encoded)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment