# Verify the signature (/fr/docs/webhook-signatures)

Every webhook includes `Psp-Signature`:

```
Psp-Signature: t=1790769600,v1=c0c9eaafdea23b0f8c992b380b286917c6459b0729b2496b8452fa695215e6b8
```

- `t` is the time of the signature, in Unix seconds.
- `v1` is a hex-encoded `HMAC-SHA256`, keyed with your secret, over `t` + `.` + the **exact** body you received.
- The key is the **full** secret, including the `whsec_` prefix.
- There can be more than one `v1` (during a rotation). It's enough for one to match.

To accept a webhook:

1. Compute the HMAC over the untouched body: don't parse or re-serialize it first.
2. Compare against each `v1` in **constant time**.
3. Reject it if `t` differs from your clock by more than **5 minutes**: that way nobody can replay an old notification to you.

<Tabs items={["Node.js", "Python", "Go"]}>
<Tab value="Node.js">

```js
const crypto = require("crypto");

// rawBody: the exact bytes received (a Buffer).
function verifyCaiboSignature(secret, header, rawBody, toleranceSeconds = 300) {
  let t = null;
  const signatures = [];
  for (const part of String(header || "").split(",")) {
    const [key, value] = part.trim().split("=", 2);
    if (key === "t") {
      if (t !== null || !/^\d+$/.test(value)) return false;
      t = value;
    } else if (key === "v1" && /^[0-9a-f]{64}$/i.test(value)) {
      signatures.push(Buffer.from(value, "hex"));
    }
  }
  if (t === null || signatures.length === 0) return false;
  const expected = crypto.createHmac("sha256", secret).update(`${t}.`).update(rawBody).digest();
  if (!signatures.some((s) => crypto.timingSafeEqual(s, expected))) return false;
  return Math.abs(Date.now() / 1000 - Number(t)) <= toleranceSeconds;
}

// Express: read the body raw, verify, then parse.
app.post("/webhooks/caibo", express.raw({ type: "application/json" }), (req, res) => {
  if (!verifyCaiboSignature(process.env.CAIBO_WEBHOOK_SECRET, req.get("Psp-Signature"), req.body)) {
    return res.sendStatus(400);
  }
  const event = JSON.parse(req.body);
  queue.push(event); // process later; dedupe on event.id
  res.sendStatus(200);
});
```

</Tab>
<Tab value="Python">

```python
import hashlib
import hmac
import re
import time

_HEX64 = re.compile(r"^[0-9a-fA-F]{64}$")

def verify_caibo_signature(secret: str, header: str, raw_body: bytes, tolerance: int = 300) -> bool:
    t, signatures = None, []
    for part in (header or "").split(","):
        key, _, value = part.strip().partition("=")
        if key == "t":
            if t is not None or not value.isdigit():
                return False
            t = value
        elif key == "v1" and _HEX64.match(value):
            signatures.append(bytes.fromhex(value))
    if t is None or not signatures:
        return False
    expected = hmac.new(secret.encode(), t.encode() + b"." + raw_body, hashlib.sha256).digest()
    if not any(hmac.compare_digest(s, expected) for s in signatures):
        return False
    return abs(time.time() - int(t)) <= tolerance

# Flask: request.get_data() is the raw body.
@app.post("/webhooks/caibo")
def caibo_webhook():
    if not verify_caibo_signature(os.environ["CAIBO_WEBHOOK_SECRET"], request.headers.get("Psp-Signature"), request.get_data()):
        return "", 400
    event = request.get_json()
    enqueue(event)  # process later; dedupe on event["id"]
    return "", 200
```

</Tab>
<Tab value="Go">

```go
package caibo

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"strconv"
	"strings"
	"time"
)

// VerifySignature reports whether header signs body with secret, within tolerance.
func VerifySignature(secret, header string, body []byte, tolerance time.Duration) bool {
	var ts string
	var signatures [][]byte
	for _, part := range strings.Split(header, ",") {
		key, value, _ := strings.Cut(strings.TrimSpace(part), "=")
		switch key {
		case "t":
			if ts != "" {
				return false
			}
			ts = value
		case "v1":
			if s, err := hex.DecodeString(value); err == nil && len(s) == sha256.Size {
				signatures = append(signatures, s)
			}
		}
	}
	t, err := strconv.ParseInt(ts, 10, 64)
	if err != nil || len(signatures) == 0 {
		return false
	}
	mac := hmac.New(sha256.New, []byte(secret))
	mac.Write([]byte(ts + "."))
	mac.Write(body)
	expected := mac.Sum(nil)
	ok := false
	for _, s := range signatures {
		ok = ok || hmac.Equal(s, expected)
	}
	age := time.Since(time.Unix(t, 0))
	return ok && age <= tolerance && age >= -tolerance
}
```

</Tab>
</Tabs>

## Test your code [#test-your-code]

With these values your function must accept the signature (ignoring the tolerance, because `t` is a past date):

| | |
| --- | --- |
| Secret | `whsec_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef` |
| Body (exact, no trailing newline) | `{"id":"evt_1","type":"payout.completed","data":{"amount":"10.00"}}` |
| `t` | `1790769600` |
| Expected signature | `t=1790769600,v1=c0c9eaafdea23b0f8c992b380b286917c6459b0729b2496b8452fa695215e6b8` |

To test against your real server, use <Endpoint method="POST" path="/v1/webhook-endpoints/{id}/ping" href="/api#tag/webhooks/POST/v1/webhook-endpoints/{id}/ping" />.

<Callout type="warn" title="The most common mistake">
Many frameworks parse the JSON before you see it. If you sign `JSON.stringify(req.body)` instead of the original body, the signature won't match. Read the **raw** body.
</Callout>
