PKCE code verifier and challenge

Runs in your browser

Generate a PKCE code_verifier and its S256 code_challenge for OAuth 2.0, or check your own. Plus state and nonce.

code_challenge (S256)
state
nonce
Authorization URL parameters
How PKCE works

Proof that the same app finishes the login

PKCE (RFC 7636, “pixie”) protects the OAuth authorization code flow for apps that can’t keep a client secret: single-page apps, mobile and desktop apps. The app makes a random code_verifier, sends its hash as code_challenge with the login request and later proves it started the flow by sending the verifier with the token request. Someone who steals the code can’t use it without the verifier.

  1. Authorize: /authorize?response_type=code&code_challenge=…&code_challenge_method=S256&state=…
  2. Token: POST /token with grant_type=authorization_code&code=…&code_verifier=…
  3. The server checks BASE64URL(SHA256(code_verifier)) == code_challenge.
In your code

Make a verifier and challenge

Browser
const verifier = btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32))))
  .replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
const hash = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier));
const challenge = btoa(String.fromCharCode(...new Uint8Array(hash)))
  .replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
Node.js
import crypto from "node:crypto";
const verifier = crypto.randomBytes(32).toString("base64url");
const challenge = crypto.createHash("sha256").update(verifier).digest("base64url");
Python
import secrets, hashlib, base64
verifier = secrets.token_urlsafe(32)
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b"=").decode()
Command line
VERIFIER=$(openssl rand -base64 32 | tr '+/' '-_' | tr -d '=')
printf '%s' "$VERIFIER" | openssl dgst -sha256 -binary | base64 | tr '+/' '-_' | tr -d '='
Common mistakes

invalid_grant and friends

Challenge with padding

The challenge is Base64url without =. One trailing = and the server compares different strings.

Hex instead of Base64url

The SHA-256 must be Base64url-encoded from the raw 32 bytes, not from the hex string.

plain method

With code_challenge_method=plain the challenge is the verifier itself. Use S256 unless the server can’t.

Verifier lost on redirect

Store the verifier in sessionStorage before redirecting; a new random value after the redirect never matches.

FAQ

Questions about the PKCE generator

What is a valid code_verifier?

A random string of 43 to 128 characters from A–Z, a–z, 0–9 and - . _ ~ (RFC 7636 §4.1). 32 random bytes in Base64url give exactly 43 characters.

Do I still need state with PKCE?

Yes, state protects against CSRF on your redirect URI and lets you match the response to the request. OpenID Connect adds nonce for the ID token.

Is PKCE only for public clients?

No. The OAuth 2.0 Security Best Current Practice (RFC 9700) and the OAuth 2.1 draft recommend PKCE for all clients, including server-side apps with a client secret.

How do I check a verifier against a challenge?

Paste the verifier into the field above. The tool shows its S256 challenge, which must equal the code_challenge your app sent.

Searches the list of pages on this site. What you type stays in your browser.