Skip to content

Instantly share code, notes, and snippets.

@onemouth
Created May 8, 2019 00:52
Show Gist options
  • Save onemouth/edf9c1943c44af8e6cd5f05a5ec4ed40 to your computer and use it in GitHub Desktop.
Save onemouth/edf9c1943c44af8e6cd5f05a5ec4ed40 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/base64"
"fmt"
"crypto/hmac"
"crypto/sha256"
)
// ValidMAC reports whether messageMAC is a valid HMAC tag for message.
func ValidMAC(message, messageMAC, key []byte) bool {
mac := hmac.New(sha256.New, key)
mac.Write(message)
expectedMAC := mac.Sum(nil)
return hmac.Equal(messageMAC, expectedMAC)
}
func main() {
claimsEncode := "eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ"
claims, _ := base64.RawURLEncoding.DecodeString(claimsEncode)
fmt.Println(string(claims))
headerEncode := "eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9"
header, _ := base64.RawURLEncoding.DecodeString(headerEncode)
fmt.Println(string(header))
keyEncode := "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"
key, _ := base64.RawURLEncoding.DecodeString(keyEncode)
sigEncode := "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
sig, _ := base64.RawURLEncoding.DecodeString(sigEncode)
msg := headerEncode + "." + claimsEncode
fmt.Println(ValidMAC([]byte(msg), sig, key))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment