import hashlib
import hmac
import re
import time


def verify(secret: str, body: bytes, signature: str, now: int | None = None) -> bool:
    match = re.fullmatch(r"t=(\d+),v1=([a-f0-9]{64})", signature)
    if match is None:
        return False
    timestamp, digest = match.groups()
    current = int(time.time()) if now is None else now
    if abs(current - int(timestamp)) > 300:
        return False
    payload = timestamp.encode("ascii") + b"." + body
    expected = hmac.new(secret.encode("utf-8"), payload, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, digest)
