Some integrations need more than a static API key. A request-signing scheme can prove that the sender knows a shared secret and that selected request components have not been altered. HMAC is a message authentication code; it does not encrypt the request, so TLS is still required.
Define a canonical message
Both sender and receiver must construct exactly the same byte sequence. A documented scheme may include the HTTP method, normalized path, selected headers, timestamp, nonce and hash of the body. Define newline rules, character encoding, URL normalization and header casing. Ambiguity between implementations is a common source of signature failures.
Calculate and compare safely
Use a standard HMAC implementation such as PHP's hash_hmac() with a modern digest such as SHA-256. Encode the output consistently, then compare signatures with hash_equals(). Never invent a cryptographic algorithm or compare signatures with ordinary string equality. Validate the timestamp and record nonces or request IDs to reduce replay attacks.
Scope keys and permissions
Issue separate secrets for each integration and environment. Give each key a clear owner, allowed operations, creation date and rotation procedure. Keep secrets outside source control and public directories. Avoid placing signatures or secrets in query strings where they may be logged.
Understand the limits
HMAC proves possession of a shared secret; it does not provide non-repudiation because both parties can generate the same signature. It also does not prevent replay unless freshness and duplicate detection are included. Use a well-reviewed provider protocol when one already exists rather than designing a custom scheme casually.
Further reading
- PHP hash_hmac — https://www.php.net/manual/en/function.hash-hmac.php
- RFC 2104: HMAC — https://www.rfc-editor.org/rfc/rfc2104