Coo*_*gle 6 javascript cryptography cryptojs
我正在尝试验证来自 Sendgrid 签名的 webhook 的签名。当前的 Sendgrid 文档仅提供 Golang 中使用 ecdsa 包的示例。
他们说这可以通过 Node crypto 包来实现,但我对加密语言没有太多的了解。
谁能帮我将当前的 Golang 代码库解析为 javascript?
// Golang Example
s := http.Request.Header.Get("X-Twilio-Email-Event-Webhook-Signature")
ts := http.Request.Header.Get("X-Twilio-Email-Event-Webhook-Timestamp")
signatureBytes, _ := base64.StdEncoding.DecodeString(s)
ecdsaSig := struct {
R *big.Int
S *big.Int
}
asn1.Unmarshal(signatureBytes, &ecdsaSig)
tsBytes := []byte(ts)
payload, _ := ioutil.ReadAll(http.Request.Body)
h := sha256.New()
h.Write(tsBytes)
h.Write(payload)
hashedPayload := h.Sum(nil)
ecdsa.Verify(publicKey, hashedPayload, ecdsaSig.R, ecdsaSig.S)
Run Code Online (Sandbox Code Playgroud)
小智 -1
我使用 sendgrid-go eventwebhook helper 进行签名验证,您可以在此处访问代码。问题是,它只能验证单个事件 webhook,因此如果多个事件(例如:在单个请求负载中处理和交付事件)验证将失败。目前在 sendgrid-nodejs 项目中报告了此错误
import (
"github.com/gin-gonic/gin"
"github.com/sendgrid/sendgrid-go/helpers/eventwebhook"
)
const verkey = "xxx"
func InsertHistory(c *gin.Context) {
var results []map[string]interface{}
if err := c.BindJSON(&results); err != nil {
fmt.Println(err)
}
// Start checking Signature
s := c.Request.Header.Get("X-Twilio-Email-Event-Webhook-Signature")
ts := c.Request.Header.Get("X-Twilio-Email-Event-Webhook-Timestamp")
payload := generatePayload(results)
publicKey, _ := eventwebhook.ConvertPublicKeyBase64ToECDSA(verkey)
b, err := eventwebhook.VerifySignature(publicKey, payload, s, ts)
if err != nil {
fmt.Println(err)
}
fmt.Printf("H.Signature: %s\nH.Timestamp: %s\nKey: %s\nPublicKey: %s\nVerified: %t", s, ts, verkey, publicKey, b)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
904 次 |
| 最近记录: |