1package extractor 2 3import ( 4 "crypto/hmac" 5 "crypto/sha256" 6 "os" 7) 8 9type KeySet struct { 10 PublicKey string 11 PrivateKey string 12} 13 14type EnvKey struct { 15 PubKeyAlias string 16 PrivKeyAlias string 17} 18 19var hashAlgo = sha256.New 20 21func KeySetFromEnv(e *EnvKey) *KeySet { 22 pubKey := os.Getenv(e.PubKeyAlias) 23 privKey := os.Getenv(e.PrivKeyAlias) 24 25 return &KeySet{ 26 PublicKey: pubKey, 27 PrivateKey: privKey, 28 } 29} 30 31func (ks *KeySet) Sign(message []byte) []byte { 32 mac := hmac.New(hashAlgo, []byte(ks.PrivateKey+ks.PublicKey)) 33 mac.Write(message) 34 return mac.Sum(nil) 35} 36 37func (ks *KeySet) Match(message, messageMAC []byte) bool { 38 expectedMAC := ks.Sign(message) 39 return hmac.Equal(expectedMAC, messageMAC) 40} 41