小编Mar*_*Tro的帖子

Golang - Dropbox webhook 签名验证 hmac

我正在编写一个需要使用 Dropbox 网络钩子的程序。我还没有找到任何已经到位的 Go 实现,所以我决定写我的。不幸的是,它似乎不起作用。

我认为这里的问题是hmac,因为我很可能做错了什么,但我似乎无法理解这里的问题到底在哪里。任何的想法?

以下是我所拥有的:

package dboxwebhook

import (
    "bytes"
    "crypto/hmac"
    "crypto/sha256"
    "errors"
    "io"
    "io/ioutil"
    "log"
)

type Signature struct {
    AppSecret []byte
    Signature []byte
}

func (w *Signature) Check(reqBody io.ReadCloser) error {

    if bytes.Compare(w.Signature, nil) == 0 {
        return errors.New("DropBox signature doesnt exist")
    }

    // building HMAC key (https://golang.org/pkg/crypto/hmac/)
    mac := hmac.New(sha256.New, w.AppSecret)
    requestBody, err := ioutil.ReadAll(reqBody)
    if err != nil {
        return err
    }

    mac.Write(requestBody)
    expectedMac := mac.Sum(nil)

    log.Println(w.AppSecret)
    log.Println(expectedMac)
    log.Println(w.Signature)

    // compare if …
Run Code Online (Sandbox Code Playgroud)

sha256 go hmac webhooks dropbox-api

1
推荐指数
1
解决办法
809
查看次数

标签 统计

dropbox-api ×1

go ×1

hmac ×1

sha256 ×1

webhooks ×1