Est.

API Key Length and Entropy Requirements by OWASP Standards

OWASP ties API key strength to entropy, not length, with concrete targets.

· 8 min read
Cover illustration for “API Key Length and Entropy Requirements by OWASP Standards”
Credential Rotation & Key Standards · November 15, 2026 · 8 min read · 1,903 words

An API key’s length says little about how secure it is. OWASP’s guidance centers on entropy, the unpredictability that makes a credential hard to guess. That distinction explains both the numbers engineers should target and the controls that must follow generation. A long string is not necessarily a strong secret, and a strong secret still needs protection after it leaves the generator.

Why entropy, not character count, is the operative security property of an API key

Developers can count characters at a glance. OWASP’s session identifier guidance asks a harder question, namely how much unpredictability those characters represent. Entropy measures that unpredictability in bits, with each additional bit doubling the number of possible values an attacker must search. Visible length describes the container. Entropy describes the uncertainty inside it.

A key made from timestamps, usernames, or counters may look long while giving an attacker little to guess. A weak pseudorandom generator creates the same problem less visibly, producing a string whose appearance conceals a predictable process. More characters from that process may leave the key weaker than its length suggests.

Encoding can also cause confusion because each alphabet holds a different amount of information in each character. A hexadecimal string and a binary string of the same length have different numbers of possible values. The same distinction applies to hexadecimal and base64 strings. Neither appearance nor alphabet establishes randomness on its own. Security depends on how the underlying value was generated and how much of its unpredictability survives in the credential.

What OWASP standards actually require, by standard and by use case

OWASP’s session management guidance sets a floor of 64 bits of entropy for session identifiers, intended to resist brute-force session guessing at typical online attack rates. That minimum applies specifically to session identifiers. Bearer keys and long-lived credentials need stronger targets because how a credential is used changes how much security it needs.

The OAuth 2.1 draft specifies that the probability of guessing a generated token must be at or below 2^(−128) and should be at or below 2^(−160). Those provisions distinguish a requirement from a recommendation. FAPI 2.0, finalized in February 2025, requires credentials not intended for end-user handling, including access tokens, refresh tokens, and authorization codes, to have enough entropy to make brute-force attacks computationally infeasible. These standards align with OWASP guidance while retaining their separate requirements.

Version 5.0 of OWASP’s application security standard was released on 30 May 2025 at Global AppSec EU Barcelona. Its V14.2.1 requirement also protects sensitive data, including API keys and session tokens, by allowing it only in HTTP message bodies or headers and keeping it out of URLs or query strings. Transmission rules do not replace entropy requirements. For production credentials, the practical minimum is 16 random bytes, while the industry recommendation for bearer keys and long-lived API credentials is 32 random bytes encoded as hex or base64url, matching AES-256 security strength.

How the choice of encoding and generation method determines whether a key actually meets those requirements

A cryptographically secure pseudorandom number generator, or CSPRNG, is mandatory because the security target assumes values an attacker cannot predict. Functions such as java.util.Random() and C’s rand() are not acceptable substitutes. Their weak, predictable algorithms are not designed for cryptographic use. Encoding their output differently does not repair the source.

Once the source is sound, encoding determines the space needed to represent its output. Hexadecimal holds four bits in each character, so it needs 32 characters for 128 bits and 64 characters for 256 bits. Base64url holds six bits in each character, so it needs 22 characters for 128 bits and 43 for 256 bits. An alphanumeric alphabet provides about 5.95 bits per character, with corresponding lengths of 22 and 43 characters. The shorter representation is not inherently more secure.

Sixteen random bytes provide 128 bits of entropy, while 32 provide 256 bits, regardless of the encoding chosen afterward. By contrast, 32 random hexadecimal characters provide only 128 bits, while 32 random bytes encoded as base64url provide 256 bits. “Generate 32 characters” leaves an important decision unstated, whereas “generate 32 random bytes” fixes the security target before presentation enters the discussion.

Why prefixes like sk_live_ are useful but cannot be counted toward entropy

Stripe’s authentication documentation distinguishes live-mode secret keys beginning with sk_live_ from test-mode keys beginning with sk_test_. That distinction is useful to operators, but the prefix is predictable. An attacker does not need to guess it. The security calculation belongs to the random body that follows.

Predictability is precisely what makes prefixes operationally valuable. An operator can distinguish production from test credentials, and an application can reject a key intended for the wrong environment. Repository and workstation scanners can use known provider patterns to selectively flag strings called “token. An alert can also identify the credential class and route a suspected leak toward the right revocation workflow.

Other formats reinforce the same distinction between recognition and secrecy. GitHub uses ghp_ for personal access tokens and gho_ for OAuth tokens, with a 6-character checksum that aids detection and contributes zero entropy. In Google Cloud’s AIzaSy format, the random body is 33-character long. AWS makes another distinction explicit through its 20-character AKIA… access key ID, which is an identifier, and its 40-character secret access key, which is the protective credential.

The design rule follows directly from these examples. Define the random body to meet the 128-bit or 256-bit target first, then prepend the prefix. Counting the prefix toward that target would exchange actual unpredictability for characters whose purpose is to be recognized.

What brute-force timelines reveal about where the real thresholds lie

Brute-force estimates become dramatic because entropy grows the search space exponentially. At an illustrative rate of one trillion guesses per second, the time to guess low-entropy keys and recommended-strength keys can range from days to longer than the age of the universe.

Rate limiting and monitoring reduce the effectiveness of online guessing, where attempts pass through the service’s controls. A leaked hash creates a different problem because offline guessing can operate at much higher speeds without those online limits. Theft of the raw key bypasses guessing altogether. These are distinct failure modes, and a brute-force estimate describes only the work of guessing.

Beyond that strength, extra length offers no additional practical security while introducing usability and compatibility problems. Longer keys are harder to copy without errors, may be truncated in logging systems, and can exceed HTTP header size limits under some server defaults. More characters are not a substitute for better controls. Once guessing is computationally infeasible, preventing disclosure and limiting misuse become the more useful investments.

How to store, hash, and rotate API keys so that entropy at generation is not wasted afterward

OWASP ASVS 5.0 §6.5.2 distinguishes how lookup secrets should be hashed according to their entropy. Below its threshold, they require an approved password storage algorithm incorporating a random salt, while stronger secrets may use a standard hash function. Hashing is required in both cases. Plaintext storage of the server-side lookup secret is not an acceptable alternative, however strong the original generation process was.

OWASP’s Cryptographic Storage guidance points to dedicated password hashing functions including Argon2, PBKDF2, Scrypt, and Bcrypt, and excludes MD5 and SHA1 as too weak for general cryptographic hashing. Password storage should not use a general hash in place of a dedicated password hashing function. High-entropy lookup secrets have a different allowance under the standard.

A split-token design applies that distinction to verification. The secret verifier portion is generated from a CSPRNG with at least 128 bits of entropy, and the server stores only its hash. The key holder presents the raw value for verification against that hash. This protects the stored verifier without pretending that the raw credential no longer needs protection wherever it is used.

Rotation addresses exposure over time, while exposure at rest requires separate protection. Production keys should rotate regularly, with service-to-service and admin keys rotating more frequently than standard keys. Generation quality does not excuse an indefinite lifetime or a failure to revoke an exposed credential. Keeping keys out of URLs and query strings, as required earlier, helps stop leaks into proxy logs, server logs, and browser history. Hashing a key in the database cannot protect the raw key if it is copied into those records.

Why the entropy principle does not change for AI agents, but the blast radius does

AI agents use the same API key mechanisms, so their credentials need the same resistance to guessing. What changes is the operational reach of a failure. An agent can act at machine speed with access inherited from a human, allowing a compromised or over-privileged key to cause damage faster and at greater scale.

Reusing a person’s session token or long-lived API key also collapses the distinction between the agent and the user in the audit trail. Revoking that credential can disrupt the person as well as the compromised agent. The appropriate authorization model gives each agent a distinct principal in the identity provider, its own credentials, scoped permissions, and its own lifecycle. Agent tokens should last minutes rather than hours, with automatic rotation. Short lifetimes limit exposure without changing the underlying entropy requirement.

The July 2026 MCP specification revision, dated 2026-07-28, standardized OAuth 2.0 Protected Resource Metadata, Resource Indicators, and a progressive authorization model, but defined no role-based access control model. Engineers still have to set up tool-level access control. Scopes provide coarse-grained permission, such as allowing transaction-history reads while blocking writes, while token claims support attribute-level decisions at the endpoint. Neither layer substitutes for the other.

Those permissions also need visibility during operation. Enterprises need real-time monitoring of which agents are running, which credentials they hold, and whether their behavior fits the access granted. Discovering a violation afterward is not equivalent to preventing it. The agent’s key may be unguessable while its permitted behavior remains dangerously broad.

What compliance and audit frameworks now require for API key governance across the full lifecycle

SOC 2 audits in 2026 treat API key hygiene, including LLM provider keys, as a credential management control. Flaws in generation, storage, rotation, and revocation can show up in audit findings as well as being engineering defects. LLM provider keys belong in a secrets manager, with a documented rotation schedule and revocation on employee offboarding. Convenience configuration is not an adequate classification for a credential.

Shadow AI exposes the organizational limit of an otherwise sound key policy. Employees who use unapproved tools can create credentials outside the company’s governance program, so those credentials go unmanaged and unrotated and never appear in its audit trail. Discovery of those tools and keys therefore belongs to the same control model as secure generation, rather than being treated as an unrelated inventory exercise.

Agent governance extends that model from credentials to actions. Auditors require a clear definition of what each agent may and may not do, complete logs of its actions, and evidence that anomalous behavior triggers detection before post-incident review. Governance platforms address that scope when they discover agents, monitor approved AI traffic, detect policy violations, enforce permissions, and produce complete audit logs. Generation-time entropy is only the starting requirement.

Logging an agent’s actions after the fact can satisfy a documentation requirement without satisfying the requirement to constrain those actions. Auditors and security teams increasingly distinguish the two. Just as character count cannot establish unpredictability, a detailed record of a violation cannot establish that a system was capable of preventing it.

Sources

  1. Insufficient Session ID Length
  2. Session Management - OWASP Cheat Sheet Series
  3. Cryptographic Storage - OWASP Cheat Sheet Series

More in Credential Rotation & Key Standards