PKCE S256: computing code_verifier and code_challenge correctly
In one line: The RFC 7636 character set and length rules, the SHA-256 to Base64URL step, and where each value travels in the authorization and token requests.
PKCE (Proof Key for Code Exchange) makes an intercepted authorization code useless. It is effectively mandatory for public clients (SPAs, mobile apps, CLIs) and recommended for confidential clients too. The computation is two lines, but one wrong character set or padding rule and the server answers invalid_grant.
1. Build the code_verifier
The verifier is a 43–128 character string from the unreserved set: A-Z a-z 0-9 - . _ ~. Spaces, +, / and = are not allowed. Encoding 32 random bytes as Base64URL gives 43 characters; 64 bytes gives 86. Both are fine, but under 43 characters violates the spec.
verifier = BASE64URL(random(32 bytes)) // e.g. dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk (43 chars)
A common mistake is building the verifier from Math.random() or a timestamp. It must be unguessable, so use crypto.getRandomValues(). Entropy matters more than length.
2. Compute the challenge
With S256 the challenge is Base64URL(SHA-256(verifier)) with the = padding removed. With plain the challenge is the verifier itself. RFC 7636 makes S256 the default and tells servers that support it to reject plain.
challenge = BASE64URL(SHA256(ASCII(verifier))) // e.g. E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM (43 chars)
Three details matter. First, hash the verifier's ASCII bytes. Second, Base64URL maps + to - and / to _ and drops padding; a plain Base64 challenge is rejected. Third, an S256 challenge is always 43 characters, so 42 or 44 means a padding or alphabet bug.
3. Where each value goes
| Step | You send | The server does |
|---|---|---|
| Authorization request | code_challenge, code_challenge_method=S256 | Stores the challenge with the code |
| Callback | code, state | Issues the code |
| Token request | grant_type=authorization_code, code, redirect_uri, client_id, code_verifier | Recomputes the challenge from the verifier and compares |
The token request must carry exactly the same redirect_uri as the authorization request. One differing character and the exchange fails even with a correct verifier.
4. Common failures
- invalid_grant — verifier mismatch, expired code, reused code or redirect_uri mismatch all map to this code. Check server logs for the specific condition.
- code_challenge required — the client is registered as public and PKCE is enforced, but no challenge was sent.
- unsupported code_challenge_method — an older server that only knows plain, or the method sent as
s256in lowercase. Method names are case sensitive. - Verifier too long — some servers enforce 128 characters strictly. 43–64 is the safe range.
5. Checking it in OAuthLab
Pick a length in panel ① and the challenge is computed immediately. Pair verification in the same panel tests a verifier against a challenge for S256 and plain. Turn on “Use challenge from ①” in the builder and the same value lands in the authorization request. All of it runs in Web Crypto and nothing is sent.
PKCE and URL checks run right here in your browser. Nothing you type is stored.
Open OAuthLabFrequently asked questions
Do I need a new verifier for every login?
Yes. A verifier is a one-time secret tied to a single authorization request. Reusing it means one leaked request exposes every later exchange.
My S256 challenge is not 43 characters. Why?
Either the Base64URL padding was not removed (44), standard Base64 characters (+/) were kept, or the hash input was not the verifier bytes. Check the encoding step until it is 43.
Can I use plain instead?
Only for legacy server compatibility. If the authorization URL leaks through logs or referrers, the verifier leaks with it and PKCE stops protecting anything.
Sources
- RFC 7636 — Proof Key for Code Exchange by OAuth Public Clients
- RFC 9700 — Best Current Practice for OAuth 2.0 Security
- OAuth 2.1 (draft-ietf-oauth-v2-1)
Read next
- Authorization URL parameters: what to send and whyauthorization_endpoint, client_id, redirect_uri, response_type, scope, state, nonce, prompt and code_challenge, with a complete example URL.
- redirect_uri registration rules and open redirect riskWhy exact matching matters, and how wildcards, subdomains, path traversal and userinfo (@) confusion turn into account takeover.
- state and nonce: stopping CSRF and ID token replayWhere to store and compare state, where nonce is verified in an ID token, length and expiry, and the multi-tab and SPA failure modes.