# Nonce & Token Generator

Generate cryptographically secure one-time tokens for web security scenarios — CSRF tokens, OAuth/OIDC state parameters, session nonces, and PKCE code_verifier/code_challenge (S256). Uses crypto.randomBytes, never Math.random.

> Canonical page: https://elysiatools.com/en/tools/nonce-generator

- **Category:** Security

- **Keywords:** nonce, csrf, oauth, state, pkce, code_challenge, code_verifier, session, token, security, crypto

## Overview

This tool generates **single-use high-entropy tokens** for specific web-security scenarios. Unlike a generic random-string generator, each scenario applies the right conventions automatically.

Scenarios:
- **CSRF Token**: an opaque token bound to a user session and submitted with state-changing requests (forms, AJAX) to prove the request came from your own page.
- **OAuth/OIDC State**: an unguessable value sent to the authorization server and validated on the redirect callback, binding the response to this request and preventing login CSRF.
- **Session Nonce**: a single-use random value tied to a session to prevent replay of a request.
- **PKCE Verifier + Challenge**: generates a `code_verifier` (kept client-side) and derives the `code_challenge = base64url(SHA256(verifier))` (sent to the auth server) per RFC 7636, so a leaked authorization code cannot be exchanged without the verifier. PKCE forces base64url encoding and a 32-96 byte verifier length (43-128 base64url chars).
- **Custom**: a generic opaque high-entropy token for any single-use scenario you define; byte length is user-controlled (8-128).

Options:
- **Byte Length**: bytes of entropy. Locked to 32 (256 bits) for all preset scenarios; user-editable (8-128) only in Custom.
- **Encoding**: base64url (URL-safe, the default for web tokens), hex, or base64. Overridden to base64url for PKCE.
- **Add expiry timestamp prefix**: prepends `base36(expireAtSeconds).token` so the server can reject expired tokens statelessly (OWASP-recommended pattern).
- **TTL (seconds)**: validity window used by the timestamp prefix.
- **Count**: generate 1-50 tokens at once.

Security: randomness comes from Node's `crypto.randomBytes()`, the cryptographically secure source — never `Math.random()`. The PKCE challenge uses SHA-256 via `crypto.createHash()`. This tool generates token material only; it does not store or validate tokens server-side.

## Inputs

- **Scenario** (select)
- **Byte Length** (number): e.g. 32
- **Encoding** (select)
- **Add expiry timestamp prefix** (checkbox)
- **TTL (seconds)** (number): e.g. 300
- **Count** (number): e.g. 1

## When to use

- When implementing OAuth 2.0 or OIDC authorization flows that require secure state parameters or PKCE code verifiers and challenges.
- When securing web forms and API endpoints against Cross-Site Request Forgery (CSRF) using unique, session-bound tokens.
- When generating single-use session nonces to prevent replay attacks or configuring Content Security Policy (CSP) headers.

## How it works

- Select your target security scenario, such as CSRF, OAuth State, Session Nonce, or PKCE, to automatically apply standard byte lengths and encoding rules.
- Configure optional settings like custom byte lengths, output encoding (base64url, hex, or base64), and stateless expiry timestamp prefixes with a defined TTL.
- The tool generates high-entropy random bytes using a cryptographically secure source (crypto.randomBytes) and computes SHA-256 hashes for PKCE challenges.
- Copy the generated tokens, verifiers, or challenges directly into your application configuration or code.

## Use cases

- Generating PKCE code verifiers and S256 challenges for mobile or single-page application OAuth authorization requests.
- Creating stateless, time-prefixed CSRF tokens to embed in HTML forms or AJAX request headers.
- Generating secure state parameters to prevent login CSRF during OIDC federated authentication redirects.

## Frequently asked questions

### How does this tool generate random values?

It uses Node's cryptographically secure crypto.randomBytes() function, ensuring high entropy and unpredictability, rather than insecure pseudo-random generators like Math.random().

### What is PKCE and how does the generator handle it?

Proof Key for Code Exchange (PKCE) prevents authorization code interception. The tool generates a secure code_verifier and derives the corresponding code_challenge using SHA-256 and base64url encoding.

### What is the purpose of the expiry timestamp prefix?

It prepends a base36-encoded expiration timestamp to the token, allowing your server to validate token expiration statelessly without querying a database.

### Why is base64url the default encoding?

Base64url is a URL-safe encoding that avoids characters like '+', '/', and '=', making it safe to pass in query parameters, headers, and cookies without URL encoding.

### Are the generated tokens stored on your server?

No. The generation process runs entirely in your browser session, and no token data is stored, logged, or transmitted to any external server.

## Related tools

- [Audio Read-Along Cue Generator](https://elysiatools.com/en/tools/audio-read-along-cue-generator): Generate sentence or word-level timestamp cues from a narration audio and its script, for karaoke-style reading and language learning.
- [Modern Unique ID Generator](https://elysiatools.com/en/tools/uid-generator): Generate modern unique identifiers: ULID, NanoID, Snowflake and CUID2. Compare formats, decode ULID timestamps and pick the right ID for your system.
- [AGEX Secure Share Note](https://elysiatools.com/en/tools/agex-secure-share-note): Generate a plain-language share note for an AGEX bundle so recipients know how to decrypt and verify it safely
- [Digital Signature Generator](https://elysiatools.com/en/tools/digital-signature-generator): Sign a message with a private key or verify a signature with a public key — supports RSA-PSS (SHA-256), ECDSA (P-256), and Ed25519. Pure client-side crypto (Node crypto.createSign/createVerify), no network. Paste a PEM key to sign or to verify a base64 signature. This tool signs/verifies; it does not generate key pairs.
- [HMAC Generator & Verifier](https://elysiatools.com/en/tools/hmac-generator-verifier): Compute an HMAC message-authentication signature over a message + shared secret using SHA-1/SHA-2/SHA-3/BLAKE2, or verify an incoming signature against the secret — with webhook presets for Stripe / Slack / GitHub and constant-time comparison
- [JWK Generator & Parser](https://elysiatools.com/en/tools/jwk-generator): Generate JSON Web Keys (JWK) for RSA, EC (P-256/P-384/P-521/secp256k1), and OKP (Ed25519/Ed448/X25519/X448), or parse an existing JWK to inspect its parameters, thumbprint, and metadata
- [Numeric OTP Generator](https://elysiatools.com/en/tools/otp-generator): Generate cryptographically secure one-time numeric codes (SMS / email verification, raffle codes, event pins) with configurable length (3-10 digits), bulk output, optional ambiguous-digit exclusion, and an optional expiry countdown. Uses crypto.randomInt, not Math.random.
- [PKCE Code Verifier & Challenge Generator](https://elysiatools.com/en/tools/pkce-code-verifier-generator): Generate, validate and verify OAuth2 / OIDC PKCE (RFC 7636) code_verifier and S256 code_challenge pairs. Three modes: (1) generate a fresh verifier + challenge from cryptographically secure random bytes at 256/384/512/768-bit entropy, (2) audit a verifier you already have against the RFC — length (43–128), charset \[A-Za-z0-9-._~\] and ≥256-bit entropy, and (3) verify a verifier/challenge pair by recomputing BASE64URL(SHA256(verifier)). Optionally build the full authorization-request URL and token-exchange body. Complements the generic nonce-generator (which only emits a verifier+challenge pair) with RFC-compliance auditing and pair verification.

## Samples

- [Android Image Processing Java Samples](https://elysiatools.com/en/samples/android-image-processing-java): Android Java image processing examples including reading/saving images, scaling, and format conversion
- [Android Image Processing Kotlin Samples](https://elysiatools.com/en/samples/android-image-processing-kotlin): Android Kotlin image processing examples including reading/saving images, scaling, and format conversion
- [Web Image Processing Python Samples](https://elysiatools.com/en/samples/web-image-processing-python): Web Python image processing examples using PIL/Pillow including reading, saving, resizing, and format conversion
- [Web Image Processing Rust Samples](https://elysiatools.com/en/samples/web-image-processing-rust): Web Rust image processing examples including image read/save, scaling, and format conversion

## Related content

- [Token, Session, and 2FA Workflow Tools](https://elysiatools.com/en/hubs/token-session-and-2fa-workflows): Debug real sign-in flows with PKCE and nonce generators, JWT and JWK inspectors, HMAC verification, OTP and TOTP testing, and auth-safe identifiers collected in one focused workflow hub.
- [JSON Schema, Mock Data, and API Fixture Generators](https://elysiatools.com/en/hubs/json-generate): Curated tools for JSON schema generation, mock payload building, and API fixture preparation in one hub.
- [CSS Effects, Palettes, and Design Token Generators](https://elysiatools.com/en/hubs/design-generate): Curated tools for CSS visual effects, palette building, and design token generation in one hub.
- [Printable PDF Layout and Template Generators](https://elysiatools.com/en/hubs/pdf-generate): Curated tools for printable PDF layout generation and reusable document templates in one hub.
