Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在验证Go中的GitHub Webhook HMAC签名_Go_Webhooks_Github Api_Hmacsha1 - Fatal编程技术网

正在验证Go中的GitHub Webhook HMAC签名

正在验证Go中的GitHub Webhook HMAC签名,go,webhooks,github-api,hmacsha1,Go,Webhooks,Github Api,Hmacsha1,我编写了以下函数,用于验证GitHub API作为webhook负载的一部分返回的X-Hub-Signature请求头 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

我编写了以下函数,用于验证GitHub API作为webhook负载的一部分返回的
X-Hub-Signature
请求头

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: %s\n", 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: %s\n", err)
        return false
    }

    expectedHash := hex.EncodeToString(hash.Sum(nil))
    log.Println("EXPECTED HASH:", expectedHash)
    return gotHash[1] == expectedHash
}
但是,这似乎不起作用,因为我无法使用正确的
密码验证。以下是一个示例输出,如果有帮助:

HUB SIGNATURE: sha1=026b77d2284bb95aa647736c42f32ea821d6894d
EXPECTED HASH: 86b6fa48bf7643494dc3a8459a8af70008f6881a
我使用了repo的逻辑作为指导,并实现了代码。然而,我无法理解这种差异背后的原因

如果有人能指出我在这里犯的小错误,我将不胜感激


请参阅:

这确实令人尴尬,但我仍想与大家分享我是如何修复它的

我输入了错误的
,造成了所有的混乱

经验教训:

  • 上面的代码片段绝对正确,可以用作验证器
  • 每个人都会犯愚蠢的错误,但只有聪明人才会承认并改正