# Check webhook signatures

> Confirm that webhook requests came from Moov by recreating the HMAC SHA-512 signature with your signing secret and comparing it to X-Signature.

> AI agents: search and read all Moov docs with the docs MCP server at https://docs.moov.io/mcp (no API key). Setup: https://docs.moov.io/guides/developer-tools/mcp-ai/docs-mcp/index.md

Every webhook request Moov sends includes an HMAC-SHA512 signature in the `X-Signature` header. Recompute it with your signing secret before you process the event, and discard the request if it doesn't match.

**Before you start:** [set up a webhook](/guides/webhooks/set-up-webhooks/index.md) and copy its signing secret from **Developers &gt; Webhooks** in the [Dashboard](https://dashboard.moov.io/developers/webhooks). Each webhook, and each mode (test or production), has its own secret.

## Verify a request

1. Read the `X-Timestamp`, `X-Nonce`, `X-Webhook-ID`, and `X-Signature` headers from the request. Most frameworks lowercase header names.
2. Build the signed payload by joining the first three values with a pipe: `{X-Timestamp}|{X-Nonce}|{X-Webhook-ID}`.
3. Compute an HMAC-SHA512 of that payload using your signing secret, and hex-encode the result.
4. Compare your value to `X-Signature` with a constant-time comparison, such as `crypto.timingSafeEqual` in Node.js or `hmac.Equal` in Go. If they match, the request came from Moov. If not, return a `4xx` and don't process the event.

**Javascript**

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

// Get your signing secret from dashboard.moov.io
const webhookSecret = process.env.WEBHOOK_SECRET;

// Check if the hash of headers match the signature
const isSigned = (timestamp = "", nonce = "", webhookId = "", signature = "") => {
  const concatHeaders = `${timestamp}|${nonce}|${webhookId}`;
  const checkHash = crypto
    .createHmac("sha512", webhookSecret)
    .update(concatHeaders)
    .digest("hex");

  // Constant-time comparison; timingSafeEqual throws if the byte lengths differ
  const got = Buffer.from(signature);
  const want = Buffer.from(checkHash);
  return got.length === want.length && crypto.timingSafeEqual(got, want);
}

// Serverless function
exports.handler = async (event) => {
  if (!event.body) {
    console.log("Invalid request");
    return { statusCode: 400, body: "Invalid request" };
  }

  // Headers are lowercased
  if (!isSigned(
    event.headers["x-timestamp"],
    event.headers["x-nonce"],
    event.headers["x-webhook-id"],
    event.headers["x-signature"])
  ) {
    console.log("Signature is invalid");
    return { statusCode: 400, body: "Signature is invalid" };
  }

  let webhook;
  try {
    webhook = JSON.parse(event.body);
  } catch (err) {
    console.log("Invalid JSON");
    return { statusCode: 400, body: "Invalid JSON" };
  }

  // Logs the event message payload
  console.log(webhook);
  return { statusCode: 200 };
};
```

**Go**

```go
package main

import (
  "crypto/hmac"
  "crypto/sha512"
  "encoding/hex"
  "fmt"
  "os"

  "github.com/aws/aws-lambda-go/events"
  "github.com/aws/aws-lambda-go/lambda"
)

func main() {
  lambda.Start(handler)
}

func handler(r events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
  var (
    webhookSecret = os.Getenv("WEBHOOK_SECRET")

    timestamp = r.Headers["x-timestamp"]
    nonce     = r.Headers["x-nonce"]
    webhookID = r.Headers["x-webhook-id"]
    gotHash   = r.Headers["x-signature"]
  )

  concatHeaders := timestamp + "|" + nonce + "|" + webhookID
  wantHash := hash([]byte(concatHeaders), []byte(webhookSecret))

  // Constant-time comparison
  if !hmac.Equal([]byte(wantHash), []byte(gotHash)) {
    msg := "Signature is invalid"
    fmt.Println(msg)

    return &events.APIGatewayProxyResponse{
      StatusCode: 400,
      Body:       msg,
    }, nil
  }

  fmt.Println("Webhook received!")

  return &events.APIGatewayProxyResponse{
    StatusCode: 200,
  }, nil
}

// hash generates a hex-encoded SHA512 HMAC of p using the secret provided.
func hash(p []byte, secret []byte) string {
  h := hmac.New(sha512.New, secret)
  h.Write(p)
  return hex.EncodeToString(h.Sum(nil))
}
```

> **Example code:**
> 
> [Fork example code](https://github.com/moovfinancial/webhook-handler)

## Next steps

- [Handle deliveries](/guides/webhooks/set-up-webhooks/index.md#handle-deliveries): respond quickly and process each event once
- [Webhook events](/guides/webhooks/webhook-events/index.md) for event types and payloads

