is_valid_webhook(payload_bytes, secret, signature) computing hmac.new(secret.encode(), payload_bytes, hashlib.sha256).hexdigest() and comparing to the given signature with hmac.compare_digest — the constant-time comparison that resists timing attacks.is_valid_webhook(b'{"amt": 5}', 'whsec_x', received_sig)True only when the HMAC matches
Recompute and compare in constant time.
import hmac, hashlib.
The secret is the HMAC key, the payload the message.