Validating GitHub Webhook HMAC signature in Go
up vote
0
down vote
favorite
I've written the following function for validating the X-Hub-Signature
request header returned by the GitHub API as part of the webhook's payload.
func isValidSignature(r *http.Request, key string) bool
// Assuming a non-empty header
gotHash := strings.SplitN(r.Header.Get("X-Hub-Signature"), "=", 2)
if gotHash[0] != "sha1"
return false
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil
log.Printf("Cannot read the request body: %sn", err)
return false
hash := hmac.New(sha1.New, byte(key))
if _, err := hash.Write(b); err != nil
log.Printf("Cannot compute the HMAC for request: %sn", err)
return false
expectedHash := hex.EncodeToString(hash.Sum(nil))
log.Println("EXPECTED HASH:", expectedHash)
return gotHash[1] == expectedHash
However, this doesn't seem to work as I'm not able to validate with the correct secret
. Here is an example output, if that helps:
HUB SIGNATURE: sha1=026b77d2284bb95aa647736c42f32ea821d6894d
EXPECTED HASH: 86b6fa48bf7643494dc3a8459a8af70008f6881a
I've used the logic from hmac-examples repo as a guideline and implemented the code. However, I am unable to understand the reason behind this discrepancy.
I would be grateful if someone can point out the trivial mistake I'm making here.
Refer: Delivery Headers
go webhooks github-api hmacsha1
add a comment |
up vote
0
down vote
favorite
I've written the following function for validating the X-Hub-Signature
request header returned by the GitHub API as part of the webhook's payload.
func isValidSignature(r *http.Request, key string) bool
// Assuming a non-empty header
gotHash := strings.SplitN(r.Header.Get("X-Hub-Signature"), "=", 2)
if gotHash[0] != "sha1"
return false
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil
log.Printf("Cannot read the request body: %sn", err)
return false
hash := hmac.New(sha1.New, byte(key))
if _, err := hash.Write(b); err != nil
log.Printf("Cannot compute the HMAC for request: %sn", err)
return false
expectedHash := hex.EncodeToString(hash.Sum(nil))
log.Println("EXPECTED HASH:", expectedHash)
return gotHash[1] == expectedHash
However, this doesn't seem to work as I'm not able to validate with the correct secret
. Here is an example output, if that helps:
HUB SIGNATURE: sha1=026b77d2284bb95aa647736c42f32ea821d6894d
EXPECTED HASH: 86b6fa48bf7643494dc3a8459a8af70008f6881a
I've used the logic from hmac-examples repo as a guideline and implemented the code. However, I am unable to understand the reason behind this discrepancy.
I would be grateful if someone can point out the trivial mistake I'm making here.
Refer: Delivery Headers
go webhooks github-api hmacsha1
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've written the following function for validating the X-Hub-Signature
request header returned by the GitHub API as part of the webhook's payload.
func isValidSignature(r *http.Request, key string) bool
// Assuming a non-empty header
gotHash := strings.SplitN(r.Header.Get("X-Hub-Signature"), "=", 2)
if gotHash[0] != "sha1"
return false
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil
log.Printf("Cannot read the request body: %sn", err)
return false
hash := hmac.New(sha1.New, byte(key))
if _, err := hash.Write(b); err != nil
log.Printf("Cannot compute the HMAC for request: %sn", err)
return false
expectedHash := hex.EncodeToString(hash.Sum(nil))
log.Println("EXPECTED HASH:", expectedHash)
return gotHash[1] == expectedHash
However, this doesn't seem to work as I'm not able to validate with the correct secret
. Here is an example output, if that helps:
HUB SIGNATURE: sha1=026b77d2284bb95aa647736c42f32ea821d6894d
EXPECTED HASH: 86b6fa48bf7643494dc3a8459a8af70008f6881a
I've used the logic from hmac-examples repo as a guideline and implemented the code. However, I am unable to understand the reason behind this discrepancy.
I would be grateful if someone can point out the trivial mistake I'm making here.
Refer: Delivery Headers
go webhooks github-api hmacsha1
I've written the following function for validating the X-Hub-Signature
request header returned by the GitHub API as part of the webhook's payload.
func isValidSignature(r *http.Request, key string) bool
// Assuming a non-empty header
gotHash := strings.SplitN(r.Header.Get("X-Hub-Signature"), "=", 2)
if gotHash[0] != "sha1"
return false
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil
log.Printf("Cannot read the request body: %sn", err)
return false
hash := hmac.New(sha1.New, byte(key))
if _, err := hash.Write(b); err != nil
log.Printf("Cannot compute the HMAC for request: %sn", err)
return false
expectedHash := hex.EncodeToString(hash.Sum(nil))
log.Println("EXPECTED HASH:", expectedHash)
return gotHash[1] == expectedHash
However, this doesn't seem to work as I'm not able to validate with the correct secret
. Here is an example output, if that helps:
HUB SIGNATURE: sha1=026b77d2284bb95aa647736c42f32ea821d6894d
EXPECTED HASH: 86b6fa48bf7643494dc3a8459a8af70008f6881a
I've used the logic from hmac-examples repo as a guideline and implemented the code. However, I am unable to understand the reason behind this discrepancy.
I would be grateful if someone can point out the trivial mistake I'm making here.
Refer: Delivery Headers
go webhooks github-api hmacsha1
go webhooks github-api hmacsha1
asked Nov 10 at 19:53
Kshitij Saraogi
1,58121744
1,58121744
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
This is really embarrasing but still I would like to share how I was able to fix it.
I sent in the wrong key
as the input which was causing all the confusion.
Lessons learnt:
- The above code snippet is absolutely correct and can be used as a validator.
- Every one makes stupid mistakes but only the wise own up to them and rectify.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242837%2fvalidating-github-webhook-hmac-signature-in-go%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This is really embarrasing but still I would like to share how I was able to fix it.
I sent in the wrong key
as the input which was causing all the confusion.
Lessons learnt:
- The above code snippet is absolutely correct and can be used as a validator.
- Every one makes stupid mistakes but only the wise own up to them and rectify.
add a comment |
up vote
1
down vote
accepted
This is really embarrasing but still I would like to share how I was able to fix it.
I sent in the wrong key
as the input which was causing all the confusion.
Lessons learnt:
- The above code snippet is absolutely correct and can be used as a validator.
- Every one makes stupid mistakes but only the wise own up to them and rectify.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This is really embarrasing but still I would like to share how I was able to fix it.
I sent in the wrong key
as the input which was causing all the confusion.
Lessons learnt:
- The above code snippet is absolutely correct and can be used as a validator.
- Every one makes stupid mistakes but only the wise own up to them and rectify.
This is really embarrasing but still I would like to share how I was able to fix it.
I sent in the wrong key
as the input which was causing all the confusion.
Lessons learnt:
- The above code snippet is absolutely correct and can be used as a validator.
- Every one makes stupid mistakes but only the wise own up to them and rectify.
answered Nov 10 at 20:12
Kshitij Saraogi
1,58121744
1,58121744
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242837%2fvalidating-github-webhook-hmac-signature-in-go%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown