SBN

HMAC Secrets Explained: Authentication You Can Actually Implement

HMAC Secrets Explained: Authentication You Can Actually Implement

HMAC (Hash-based Message Authentication Code) secrets are the industry standard for webhook signatures, internal API authentication, and session tokens. They provide a fast, simple way to verify that a message hasn't been altered and came from a trusted source.

While services like Stripe, GitHub, and Slack make HMAC easy to consume, implementing it securely requires attention to detail. This guide covers how HMAC works, how to implement it correctly, and how to avoid common security pitfalls like timing attacks and hardcoded secrets.

What Is an HMAC Secret?

An HMAC secret is a shared cryptographic key used to generate and verify message authentication codes. These act as a digital signature that proves a message hasn't been modified and originated from a trusted source.

Unlike public-key cryptography (which uses two different keys), HMAC uses a single symmetric key known only to the sender and receiver. This secret is a high-entropy value (usually a 256-bit random string) that requires special caution. Combined with the message (the raw data being authenticated, e.g., a webhook payload or API body), it gets hashed through a cryptographic algorithm like SHA-256 to produce the signature.

Common Use Cases:

  • Webhook Verification: Verifying that events from Stripe, GitHub, or Slack are legitimate.
  • API Authentication: Securing internal service-to-service communication.
  • Session Tokens: Signing cookies or JWTs (HS256) to prevent client-side tampering.

How HMAC Works

Here is an important fact: HMAC is more than just hash(key + message). That simple approach is vulnerable to length extension attacks. Instead, HMAC (defined in RFC 2104) uses a two-pass "hash-of-hashes" construction:

  1. Inner Hash: The key is XORed with a constant (ipad) and hashed with the message.
  2. Outer Hash: The key is XORed with a different constant (opad) and hashed with the result of the inner hash.

This structure ensures cryptographic strength even if the underlying hash function has minor weaknesses. As a developer, you don't need to implement this: standard libraries handle it. Your focus should be on key management and secure verification (more on that later).

Implementing HMAC Authentication

Generating Signatures

Always use your language's standard crypto library. Never roll your own crypto.

Python:

import hmac
import hashlib
def generate_signature(secret: str, payload: str) -> str:
    return hmac.new(
        secret.encode('utf-8'),
        payload.encode('utf-8'),
        hashlib.sha256
    ).hexdigest()

Node.js:

const crypto = require('crypto');
function generateSignature(secret, payload) {
  return crypto
    .createHmac('sha256', secret)
    .update(payload)
    .digest('hex');
}

Verifying Signatures (Crucial!)

Verification is where most vulnerabilities occur. You must use constant-time comparison to prevent timing attacks: Standard string comparisons (==) stop as soon as they find a mismatch. An attacker can measure how long the server takes to respond and guess the signature one byte at a time.

To prevent these attacks, it is crucial to use comparison functions that always take the same amount of time, regardless of where the mismatch occurs.

Python (Flask Example):

import hmac
import hashlib
from flask import Flask, request, abort
app = Flask(__name__)
# In production, load this from a secure secrets manager
WEBHOOK_SECRET = "your-secure-random-hex-string"
@app.route('/webhook', methods=['POST'])
def handle_webhook():
    signature = request.headers.get('X-Signature')
    # Verify the RAW body bytes, not parsed JSON
    payload = request.get_data()
    
    expected = hmac.new(
        WEBHOOK_SECRET.encode(), 
        payload, 
        hashlib.sha256
    ).hexdigest()
    
    # SECURE: Constant-time comparison
    if not hmac.compare_digest(signature, expected):
        abort(403)
        
    return "Verified", 200

Node.js (Express Example):

const express = require('express');
const crypto = require('crypto');
const app = express();
// Middleware to save raw body for verification
app.use(express.json({
  verify: (req, res, buf) => { req.rawBody = buf; }
}));
app.post('/webhook', (req, res) => {
  const signature = req.headers['x-signature'];
  const expected = crypto
    .createHmac('sha256', process.env.WEBHOOK_SECRET)
    .update(req.rawBody)
    .digest('hex');
  // SECURE: Constant-time comparison
  const sigBuffer = Buffer.from(signature, 'hex');
  const expBuffer = Buffer.from(expected, 'hex');
  
  if (sigBuffer.length !== expBuffer.length || 
      !crypto.timingSafeEqual(sigBuffer, expBuffer)) {
    return res.status(403).send('Invalid signature');
  }
  res.send('Verified');
});

Security Best Practices

1. Key Management & Storage

Your HMAC security is only as good as your secret key.

  • Generate Strong Keys: Use a cryptographically secure random number generator. Keys should be at least 32 bytes (256 bits).
    • openssl rand -hex 32
  • Never Commit Secrets: Hardcoding secrets in source code is the #1 cause of data breaches. Use environment variables or dedicated secrets managers (AWS Secrets Manager, HashiCorp Vault).
  • Scan for Leaks: Use tools like GitGuardian to automatically detect if an HMAC secret is accidentally committed to your repository.

2. Prevent Replay Attacks

A valid signature is valid forever unless you add a timestamp. An attacker could capture a legitimate request and "replay" it later.

  • Include a Timestamp: Send a timestamp header (e.g., X-Timestamp).
  • Verify Age: In your verification logic, reject requests older than 5 minutes.
  • Sign the Timestamp: Include the timestamp in the HMAC signature payload so it can't be modified.

Note: Timestamps reduce the replay window but don't eliminate it entirely. For high-security scenarios (financial transactions, sensitive mutations), consider nonce-based replay prevention where the server issues a one-time token that's included in the signature and invalidated after use.

3. Prevent Destination Replay (Context Binding)

For internal APIs, signing only the body isn't enough. An attacker could intercept a valid request to /api/user/preferences and replay it to /api/admin/create if the bodies are compatible.

  • Bind Context: Sign a "canonical string" that includes the host, HTTP method, and path, not just the body.
    • Example: HMAC(secret, host + "POST" + "/api/v1/resource" + timestamp + body)

4. Choose the Right Algorithm

  • Recommended: HMAC-SHA256. It's widely supported, fast, and secure.
  • Acceptable: HMAC-SHA1 is theoretically still secure for HMAC constructions (unlike direct hashing), but it's best to avoid it to prevent compliance flags.
  • Avoid: HMAC-MD5.

5. API Key Security

If you are using HMAC for API authentication, treat the shared secrets with the same care as API keys.

  • Rotate: Change secrets periodically. Use a dual-phase approach (supporting both old and new keys simultaneously) to rotate without downtime.
  • Scope: If using multiple HMAC keys, bind each to specific clients or API scopes to limit blast radius if one is compromised.

Learn more about API Key Security Best Practices.

HMAC vs. JWT vs. OAuth

Developers often ask which approach fits their use case. 

In reality these aren’t strictly comparable: HMAC is a cryptographic primitive, JWT is a token format, and OAuth is an authorization framework.

The table below might be useful for you depending on your practical needs:

Feature

HMAC

JWT (HS256)

OAuth 2.0

Key Type

Symmetric (Shared Secret)

Symmetric or Asymmetric

Asymmetric (Tokens)

Complexity

Low

Medium

High

Best For

Webhooks, Internal APIs

Stateless Sessions

User Login, 3rd Party Access

Non-repudiation

No

No (HS256) / Yes (RS256)

Yes

Use HMAC when you control both ends of the connection or have a direct 1:1 relationship (like a webhook provider). Use OAuth for user-facing applications and JWTs for distributed session management.

Troubleshooting Checklist

If your signatures aren't matching:

  1. Check Encoding: Are you hashing the hex string or the raw bytes of the key? (Usually raw bytes).
  2. Raw Body: Are you hashing the exact raw body received? Even a single missing space or newline will break the signature. Do not re-serialize parsed JSON.
  3. Algorithm: Confirm both sides are using the same hash function (e.g., SHA-256).
  4. Headers: Ensure you are extracting the signature from the correct header key (case-sensitivity matters).

Conclusion

HMAC provides a robust, efficient way to authenticate messages between trusted parties. By generating strong keys, using constant-time verification, and protecting your secrets from being hardcoded, you can secure your webhooks and APIs against tampering and impersonation.

Ready to secure your secrets? Start by scanning your repositories for hardcoded keys with GitGuardian to ensure your HMAC secrets remain private.

Frequently Asked Questions (FAQ)

How do I find my HMAC secret key?

If you are verifying webhooks from a service like Stripe or GitHub, the HMAC secret is generated by the provider. You can typically find it in their developer dashboard under "Webhooks" or "API Security." If you are implementing your own HMAC system, you must generate a cryptographically strong random key yourself using a secure random number generator (e.g., openssl rand -hex 32).

What is HMAC (for dummies)?

Think of an HMAC as a digital wax seal on an envelope.

  1. The message is the letter inside.
  2. The secret key is the unique signet ring used to press the wax.
  3. The signature is the resulting wax seal.Only someone with the exact same signet ring (the secret key) can create a matching seal. If the letter is opened or changed, the seal breaks, and the receiver knows it was tampered with.

Is HMAC-SHA1 still secure?

Yes, HMAC-SHA1 is generally considered secure for message authentication, unlike SHA-1 for digital signatures or file hashing. The "double-hashing" structure of HMAC protects it from the collision attacks that broke SHA-1. However, for all new applications, industry best practices recommend using HMAC-SHA256 to ensure long-term security and compliance.

Avatar photo

Thomas Segura

What You Need to Scale AppSec Thomas Segura - Content Writer @ GitGuardian Author Bio Thomas has worked both as an analyst and as a software engineer consultant for various big French companies. His passion for tech and open source led him to join GitGuardian as technical content writer. He focuses now on clarifying the transformative changes that cybersecurity and software are going through. Website:https://www.gitguardian.com/ Twitter handle: https://twitter.com/GitGuardian Linkedin: https://www.linkedin.com/company/gitguardian Introduction Security is a dilemma for many leaders. On the one hand, it is largely recognized as an essential feature. On the other hand, it does not drive business. Of course, as we mature, security can become a business enabler. But the roadmap is unclear. With the rise of agile practices, DevOps and the cloud, development timeframes have been considerably compressed, but application security remains essentially the same. DevSecOps emerged as an answer to this dilemma. Its promise consists literally in inserting security principles, practices, and tools into the DevOps activity stream, reducing risk without compromising deliverability. Therefore there is a question that many are asking: why isn't DevSecOps already the norm? As we analyzed in our latest report DevSecOps: Protecting the Modern Software Factory, the answer can be summarized as follows: only by enabling new capacities across Dev, Sec and Ops teams can the culture be changed. This post will help provide a high-level overview of the prerequisite steps needed to scale up application security across departments and enable such capabilities. From requirements to expectations Scaling application security is a company-wide project that requires thorough thinking before an y decision is made. A first-hand requirement is to talk to product and engineering teams to understand the current global AppSec maturity. The objective at this point is to be sure to have a comprehensive understanding of how your products are made (the processes, tools, components, and stacks involved). Mapping development tools and practices will require time to have the best visibility possible. They should include product development practices and the perceived risk awareness/appetite from managers. One of your objectives would be to nudge them so they take into account security in every decision they make for their products, and maybe end up thinking like adversaries. You should be able to derive security requirements from the different perceptual risks you are going to encounter. Your job is to consolidate these into a common set for all applications, setting goals to align the different teams collaborating to build your product(s). Communicating transparently with all relevant stakeholders (CISO, technical security, product owner, and development leads) about goals and expectations is essential to create a common ground for improvement. It will be absolutely necessary to ensure alignment throughout the implementation too. Open and accessible guardrails Guardrails are the cornerstone of security requirements. Their nature and implementation are completely up to the needs of your organization and can be potentially very different from one company to the other (if starting from scratch, look no further than the OWASP Top10). What is most important, however, is that these guardrails are open to the ones that need them. A good example of this would be to centralize a common, security-approved library of open-source components that can be pulled from by any team. Keep users' accessibility and useability as a priority. Designing an AppSec program at scale requires asking “how can we build confidence and visibility with trusted tools in our ecosystem?”. For instance, control gates should never be implemented without considering a break-glass option (“what happens if the control is blocking in an emergency situation?”). State-of-the-art security is to have off-the-shelf secure solutions chosen by the developers, approved by security, and maintained by ops. This will be a big leap forward in preventing vulnerabilities from creeping into source code. It will bring security to the masses at a very low cost (low friction). But to truly scale application security, it would be silly not to use the software engineer's best ally: the continuous integration pipeline. Embed controls in the CI/CD AppSec testing across all development pipelines is the implementation step. If your organization has multiple development teams, it is very likely that different CI/CD pipelines configurations exist in parallel. They may use different tools, or simply define different steps in the build process. This is not a problem per se, but to scale application security, centralization and harmonization are needed. As illustrated in the following example CI/CD pipeline, you can have a lot of security control steps: secrets detection, SAST, artifact signing, access controls, but also container or Infrastructure as Code scanning (not shown in the example) (taken from the DevSecOps whitepaper) The idea is that you can progressively activate more and more control steps, fine-tune the existing ones and scale both horizontally and vertically your “AppSec infrastructure”, at one condition: you need to centralize metrics and controls in a stand-alone platform able to handle the load corresponding to your organization’s size. Security processes can only be automated when you have metrics and proper visibility across your development targets, otherwise, it is just more burden on the AppSec team's shoulders. In turn, metrics and visibility help drive change and provide the spark to ignite a cultural change within your organization. Security ownership shifts to every engineer involved in the delivery process, and each one is able to leverage its own deep (yet partial) knowledge of the system to support the effort. This unlocks a world of possibilities: most security flaws can be treated like regular tickets, rule sets can be optimized for each pipeline based on criticality, capabilities or regulatory compliance, and progress can be tracked (saved time, avoided vulnerabilities etc.). In simpler terms, security can finally move at the DevOps speed. Conclusion Security can’t scale if it’s siloed, and slowing down the development process is no longer an option in a world led by DevOps innovation. The design and implementation of security controls are bound to evolve. In this article, we’ve depicted a high-level overview of the steps to be considered to scale AppSec. This starts with establishing a set of security requirements that involve all the departments, in particular product-related ones. From there it becomes possible to design guardrails to make security truly accessible with a mix of hard and soft gates. By carefully selecting automated detection and remediation that provide visibility and control, you will be laying a solid foundation for a real model of shared responsibility for security. Finally, embedding checks in the CI/CD system can be rolled out in multiple phases to progressively scale your security operations. With automated feedback in place, you can start incrementally adjusting your policies. A centralized platform creates a common interface to facilitate the exchange between application security and developer teams while enforcing processes. It is a huge opportunity to automate and propagate best practices across teams. Developers are empowered to develop faster with more ownership. When security is rethought as a partnership between software-building stakeholders, a flywheel effect can take place: reduced friction leads to better communication and visibility, automating of more best practices, easing the work of each other while improving security with fewer defects. This is how application security will finally be able to scale through continuous improvement.

thomas-segura has 68 posts and counting.See all posts by thomas-segura