Created
January 11, 2015 17:59
-
-
Save lxfontes/4d3474d863723970b7e0 to your computer and use it in GitHub Desktop.
Github Signature verification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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