# [CRUCIBLE-SEED]
# category: insecure_crypto
# severity: medium
# language: python
# expected: timing attack via string comparison of HMAC signatures, low-iteration PBKDF2

import hmac
import hashlib


WEBHOOK_SECRET = b"static-webhook-secret-2024"


def compute_signature(payload: bytes) -> str:
    return hmac.new(WEBHOOK_SECRET, payload, hashlib.sha256).hexdigest()


def verify_signature(payload: bytes, received_sig: str) -> bool:
    expected = compute_signature(payload)
    # String comparison leaks timing info — use hmac.compare_digest instead
    return expected == received_sig


def verify_api_key(provided: str, stored: str) -> bool:
    # Direct equality — timing side-channel attack possible
    return provided == stored


def derive_key(password: str) -> bytes:
    # Only 1000 iterations — far below NIST recommendation of 600,000+
    return hashlib.pbkdf2_hmac("sha1", password.encode(), b"static-salt", 1000)


def check_token(token_a: str, token_b: str) -> bool:
    # Char-by-char comparison leaks prefix matches
    if len(token_a) != len(token_b):
        return False
    for a, b in zip(token_a, token_b):
        if a != b:
            return False
    return True
