Skip to content

Instantly share code, notes, and snippets.

@lxfontes
Created January 11, 2015 17:59
Show Gist options
  • Select an option

  • Save lxfontes/4d3474d863723970b7e0 to your computer and use it in GitHub Desktop.

Select an option

Save lxfontes/4d3474d863723970b7e0 to your computer and use it in GitHub Desktop.
Github Signature verification
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"errors"
)
func verifyGithubSignature(secret string, payload []byte, signature string) error {
if len(signature) < 5 {
return errors.New("Github Signature is too small")
}
// skip 'sha1='
expected, err := hex.DecodeString(string(signature[5:]))
if err != nil {
return err
}
expected = []byte(expected)
mac := hmac.New(sha1.New, []byte(secret))
mac.Write(payload)
actual := mac.Sum(nil)
if !hmac.Equal(actual, expected) {
return errors.New("Invalid Github signature")
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment