Skip to content

PKCE code verifier and challenge generator

Generate a random PKCE code verifier and its S256 code challenge, ready to add to your authorization and token requests.

Runs in your browser. Nothing you paste is sent to us.

Code verifier

Any length from 43 to 128 works. Each character adds about 6 bits of randomness.

Made in your browser with Web Crypto. You can also paste your own to compute its challenge.

code_challenge_method

Result

code_challenge
 
method
S256
verifier
0 characters
challenge
0 characters
Add to the authorization request
 
Add to the token request
 
Keep the verifier in your app until the user comes back, then send it with the code. Never put it in the authorization URL.

How to use it

  1. A new code verifier is made when the page opens. Press Generate new for another, or pick a length.
  2. Copy the code_challenge and code_challenge_method=S256 into the authorization request.
  3. Keep the code_verifier in your app until the user comes back.
  4. Send the code_verifier with the code to the token endpoint.

What to know

PKCE (RFC 7636) stops a stolen authorization code from being used. The app keeps a random verifier, sends only its SHA-256 hash with the authorization request, and proves it holds the verifier when it redeems the code.

The challenge is BASE64URL(SHA256(verifier)), without padding. Press RFC 7636 example to check the tool against the test vector in the RFC’s Appendix B.

Keycloak supports PKCE on every client, and a client can be set to require S256 in its settings. RFC 9700 recommends PKCE for every client, confidential ones too.

What PKCE is and why it exists

PKCE stands for Proof Key for Code Exchange. It is defined in RFC 7636 and is pronounced "pixy". It adds one secret to the authorization code flow: a random string that only the app knows, called the code verifier.

The problem it solves is a stolen authorization code. In the code flow, the code travels through the browser in the redirect URL. On a phone, another app can register the same custom URL scheme and receive it. On the web, a code can leak through logs, a referrer header or a malicious script. Without PKCE, whoever holds the code can redeem it at the token endpoint, as long as the client has no secret.

With PKCE, the code alone is useless. The token endpoint only hands out tokens to the party that can show the original verifier, and the verifier never left the app.

How PKCE works, step by step

  1. The app makes a random code_verifier of 43 to 128 characters and keeps it.
  2. It hashes the verifier with SHA-256 and encodes the hash as Base64url without padding. That is the code_challenge.
  3. The authorization request carries code_challenge and code_challenge_method=S256.
  4. The server stores the challenge with the authorization code it issues.
  5. The app sends the code and the original code_verifier to the token endpoint.
  6. The server hashes the verifier, compares it with the stored challenge, and issues tokens only if they match.

The formula from RFC 7636 section 4.2 is code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier))). The test vector in the RFC’s Appendix B is a good way to check any implementation.

RFC 7636 Appendix B example
code_verifier  = dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
code_challenge = E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
Where each value goes (sample values)
GET /realms/acme/protocol/openid-connect/auth
  ?response_type=code&client_id=web-app
  &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
  &code_challenge_method=S256&...

POST /realms/acme/protocol/openid-connect/token
  grant_type=authorization_code&code=SAMPLE_CODE
  &code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk&...

What this tool does

The generator makes a code verifier in your browser with Web Crypto and computes its challenge. Nothing is sent to a server.

  • It picks characters from the unreserved set RFC 7636 allows: letters, digits and -, ., _, ~.
  • It avoids modulo bias, so every character is equally likely.
  • It lets you choose a length of 43, 64, 96 or 128 characters.
  • It computes the S256 challenge, or shows the plain method with a warning.
  • It accepts a verifier you paste and checks its length and characters.
  • It loads the RFC 7636 example and confirms the result matches Appendix B.

It also prints the two snippets you need: the parameters for the authorization request and the one for the token request.

What it does not do

It does not store the verifier, send a request, or talk to your server. Your app must keep the verifier between the redirect and the callback. For a full authorization URL with PKCE already built in, use the authorization URL builder.

How to use the generator

  1. Open the page. A new 64-character verifier is ready.
  2. Pick a different length if your library expects one, or press Generate new.
  3. Leave the method on S256.
  4. Copy the authorization request parameters into your test URL.
  5. Keep the verifier and send it with the code to the token endpoint.

To test your own code, paste the verifier your app made. If the challenge here differs from the one your app sent, your encoding is wrong. The usual causes are standard Base64 instead of Base64url, or padding left on the end.

Where PKCE is used

RFC 9700, the OAuth 2.0 security best current practice, recommends PKCE for every client that uses the authorization code flow.

Single-page apps

A browser app cannot keep a client secret, so it is a public client. PKCE is what ties the code to the app that started the sign-in. Keep the verifier in memory or session storage for the length of the redirect only.

Mobile and desktop apps

RFC 8252 asks native apps to sign in through the system browser and to use PKCE. Custom URL schemes can be claimed by other apps, so the verifier is the real protection.

Server-side web apps

A confidential client has a secret, but PKCE still helps. It stops an attacker from injecting a stolen code into a victim’s session, which a client secret does not prevent.

Command-line tools

A CLI that opens the browser and listens on a loopback port uses the same pattern as a desktop app. Where no browser is available, the device flow fits better.

How Keycloak handles PKCE

Keycloak supports PKCE on every OpenID Connect client. If the authorization request carries a code_challenge, Keycloak checks the code_verifier when the code is redeemed.

A client can also require PKCE. Set its PKCE method to S256 in the client settings. Keycloak then rejects authorization requests without a challenge, with the error description Missing parameter: code_challenge_method.

The endpoints are https://<host>/realms/<realm>/protocol/openid-connect/auth for the challenge and https://<host>/realms/<realm>/protocol/openid-connect/token for the verifier.

Common PKCE errors and fixes

SymptomLikely causeFix
invalid_grant at the token endpointThe verifier does not hash to the stored challengeSend the same verifier you hashed; check Base64url and no padding
invalid_request on the authorization requestThe client requires PKCE and no challenge was sentAdd code_challenge and code_challenge_method=S256
Code works once, then failsCodes are single-useStart a new authorization request
Verifier rejected as invalidToo short, too long, or a forbidden characterUse 43 to 128 unreserved characters

A frequent bug is a new verifier on every page load. The app makes a verifier, redirects, and on return makes another one. Store the verifier before the redirect and read it back on the callback.

S256 or plain

RFC 7636 defines two methods. With S256 the challenge is a hash. With plain the challenge is the verifier itself.

Plain gives no protection when the authorization request can be seen, which is exactly the case PKCE is meant for. The RFC allows plain only when a client cannot compute SHA-256. Every current browser and platform can, so use S256.

Making PKCE values in your own code

Most OpenID Connect libraries make the verifier and challenge for you. If you write it yourself, the browser and Node.js both have what you need. The sketch below is a sample, not a drop-in library.

Sample: S256 challenge with Web Crypto
const bytes = crypto.getRandomValues(new Uint8Array(32));
const verifier = base64url(bytes); // 43 characters
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier));
const challenge = base64url(new Uint8Array(hash));

The base64url helper must replace + with - and / with _, and drop any = padding. Thirty-two random bytes give a 43-character verifier, the shortest RFC 7636 allows, with 256 bits of randomness.

Test the helper against the Appendix B example before you ship it. If your result for the example verifier is not E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM, the encoding is off. A wrong helper fails only at the token endpoint, where the error says little more than invalid_grant.

Security notes

  • Make the verifier with a cryptographic random source, such as crypto.getRandomValues, never Math.random.
  • Use a new verifier for every authorization request.
  • Never put the verifier in the authorization URL or a log.
  • PKCE does not replace state for your own session logic, or nonce for ID tokens. See the state and nonce generator.
  • PKCE protects the code, not the tokens. Treat tokens as secrets once issued.
Questions

Frequently asked questions

What is a PKCE code verifier?

A random string of 43 to 128 characters that the app makes before the authorization request and keeps secret. It may use letters, digits and the characters - . _ ~. The app sends it to the token endpoint to prove it started the sign-in.

How is the code challenge calculated?

Take the SHA-256 hash of the verifier’s ASCII bytes and encode it as Base64url without padding. The result is always 43 characters. The RFC 7636 example verifier gives E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM.

Is PKCE required for confidential clients?

The core OAuth 2.0 spec does not require it, but RFC 9700 recommends it for all clients. It protects against code injection, which a client secret does not stop. Turning it on costs one hash per sign-in.

Why does my token request fail with invalid_grant after adding PKCE?

Most often the verifier sent to the token endpoint is not the one that was hashed. Check that you store it before the redirect and read the same value back. Also check that the challenge uses Base64url without padding.

Does PKCE replace the state parameter?

PKCE protects the code against theft and injection. state is still the usual way to match the callback to the session that started it and to carry your own return information. Most apps send both.

Can I reuse a code verifier?

No. Make a new one for each authorization request. Reusing it would let a code from one attempt be redeemed with a verifier seen in another.

How do I make Keycloak require PKCE?

Set the client’s PKCE method to S256 in its settings. Keycloak then rejects authorization requests that carry no code challenge.

What length should a PKCE code verifier be?

Anything from 43 to 128 characters is valid. RFC 7636 suggests 32 random bytes encoded as Base64url, which gives 43 characters. Longer verifiers add no practical security but do no harm.

Is it safe to generate a code verifier on a website?

This page makes the verifier in your browser and sends nothing. For production, your app should make its own verifier at runtime. Use generated values here for testing and learning.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.