pkce
Generate OAuth 2.0 PKCE code verifiers and challenges in Rust per RFC 7636.
Repository Health
Technical Analysis
pkce is a minimal Rust library for generating the code verifiers and code challenges used by OAuth 2.0 Proof Key for Code Exchange (PKCE, RFC 7636). It exposes two functions: one to produce a cryptographically random verifier of a chosen length, and one to derive the corresponding SHA-256, base64url-encoded challenge.
PKCE hardens the OAuth authorization-code flow against interception attacks, and is required for public clients such as native and single-page apps. This crate keeps the implementation deliberately tiny and dependency-light, with optional WebAssembly support via getrandom so it can run in the browser.
What You Get
- code_verifier(length) to generate a spec-compliant random PKCE verifier
- code_challenge(&verifier) to derive the SHA-256, base64url-encoded challenge
- RFC 7636 S256 challenge method implemented correctly out of the box
- Optional WebAssembly support via the js feature (getrandom)
- A tiny, dependency-light API with no framework lock-in
Common Use Cases
- Adding PKCE to an OAuth 2.0 authorization-code flow in a Rust client
- Securing native or CLI apps that cannot keep a client secret
- Generating PKCE pairs in browser/WASM single-page apps
- Building an OAuth client library that needs standards-compliant PKCE
Under The Hood
Architecture - The entire crate is a single src/lib.rs exposing two public functions. code_verifier(length) asserts the length is within the RFC 7636 range and builds a random string from the unreserved character set using rand’s thread_rng. code_challenge(&verifier) hashes the verifier with SHA-256 via sha2 and passes it through a private base64_url_encode helper that produces the URL-safe, unpadded encoding the spec mandates. There is no state, configuration, or abstraction layer beyond these functions.
Tech Stack - Rust 2018 edition with three small dependencies: base64 for encoding, rand for the random verifier, and sha2 for the challenge hash. An optional js Cargo feature pulls in getrandom with its js backend to support WebAssembly targets where the default OS RNG is unavailable.
Code Quality - The code is short, readable, and correct for the S256 method, with doc comments and an integration test in tests/tests.rs plus a doctest in the module header. The verifier length is validated with an assert that panics outside 43-128 bytes, matching the spec, though a Result-returning variant would be a friendlier API. The project is inactive (last updated 2023) but the surface is small enough to be effectively complete.
API Design - The API is about as simple as possible: two free functions with no builders, structs, or setup, so a caller can generate a valid PKCE pair in two lines. The main sharp edge is that an out-of-range verifier length panics rather than returning an error, so callers must respect the 43-128 byte bound. Documentation is a concise README and rustdoc with a runnable example, keeping the learning curve near zero.