Skip to content
Developer Docs

Signature Verification ​

Each webhook is signed with HMAC-SHA256 using a webhookSecret unique to your organisation. Your endpoint must recompute the signature to confirm the event is authentic.

The algorithm ​

text
HMAC-SHA256( secret, rawBody + timestamp ) → hex → Base64
HeaderDescription
zb-timestampUnix milliseconds at send time
zb-signatureSignature generated by Zertiban

Sign the body exactly as received

Zertiban signs the body exactly as it sends it: compact JSON, with no whitespace or line breaks, and the keys in the order they travel in, which is not alphabetical. Compute the signature over the bytes you receive, without parsing the JSON, re-serializing it or converting it to text. Reordering the keys, adding whitespace or escaping characters such as ñ (\u00f1) yields a different signature.

Detailed steps ​

  1. Read the raw body, as bytes, before any automatic framework parsing.
  2. Concatenate rawBody + zb-timestamp, using the header value exactly as received.
  3. HMAC-SHA256(webhookSecret, step2) over the UTF-8 bytes → binary digest.
  4. hex(digest) → bytes → Base64 = expected value of zb-signature.

Implementations ​

java
import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;

// rawBody: the body bytes exactly as received, not parsed or converted to a String
public boolean isValidWebhook(byte[] rawBody, String timestamp,
                               String signature, String secret) throws Exception {
    if (rawBody == null || timestamp == null || signature == null) {
        return false;
    }
    // 1. HMAC-SHA256 of message = raw body + timestamp
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
    mac.update(rawBody);
    byte[] digest = mac.doFinal(timestamp.getBytes(StandardCharsets.UTF_8));

    // 2. hex → Base64
    String expected = Base64.getEncoder()
        .encodeToString(Hex.encodeHexString(digest)
            .getBytes(StandardCharsets.UTF_8));

    return MessageDigest.isEqual(
        expected.getBytes(StandardCharsets.UTF_8),
        signature.getBytes(StandardCharsets.UTF_8));
}
// Spring: take the body as @RequestBody byte[], not as a String or a DTO
javascript
const crypto = require('crypto');

// rawBody: the body exactly as received, as a Buffer, no JSON.parse
function isValidWebhook(rawBody, timestamp, signature, secret) {
  if (!rawBody || !timestamp || !signature) return false;
  const hex = crypto.createHmac('sha256', secret).update(rawBody).update(timestamp).digest('hex');
  const expected = Buffer.from(Buffer.from(hex).toString('base64'));
  const received = Buffer.from(signature);
  // timingSafeEqual throws if the lengths differ
  return expected.length === received.length && crypto.timingSafeEqual(expected, received);
}
// Express: use express.raw({ type: 'application/json' }) on the webhook route, not express.json()
python
import hmac, hashlib, base64

# raw_body: the body exactly as received, as bytes, no json.loads
def is_valid_webhook(raw_body, timestamp, signature, secret):
    if not raw_body or not timestamp or not signature:
        return False
    digest = hmac.new(
        secret.encode(), raw_body + timestamp.encode(), hashlib.sha256
    ).digest()
    expected = base64.b64encode(digest.hex().encode())
    return hmac.compare_digest(expected, signature.encode())

# Flask: use request.get_data() to read the raw body as bytes

Best practices ​

  • Validate before processing. Never process a webhook without first verifying its signature.
  • Constant-time comparison. Use constant-time comparison (hmac.compare_digest, MessageDigest.isEqual, crypto.timingSafeEqual) to avoid timing attacks.
  • Reject invalid signatures with 401 or 403, don't process the event, and log the attempt for auditing.
  • Validate the time window. Only accept if zb-timestamp is within ±5 minutes to prevent replay attacks.

Troubleshooting: signature does not verify ​

CauseSolution
Body parsed, reformatted or converted to text before verificationUse the raw bytes: express.raw() in Express, request.get_data() in Flask, @RequestBody byte[] in Spring
Wrong webhookSecretConfirm the secret with Zertiban
webhookSecret decodedUse it as is, as text. Even if it looks like Base64, don't decode it
Missing hex→Base64 stepThe algorithm is HMAC → hex → Base64, not HMAC → Base64 directly
zb-timestamp omittedAlways concatenate rawBody + timestamp, in that order, before the HMAC

Cleanly separate the four phases of webhook consumption:

  1. Reception of the HTTP request.
  2. Cryptographic validation of signature and timestamp.
  3. Persistence of the event (for idempotency and auditing).
  4. Functional processing, asynchronously.

This yields consumers that are idempotent, auditable, and resilient to retries, decoupled from business processing.