Secure Token Generator — Free HTML CSS JS Snippet

Secure Token Generator · Forms · Plain HTML, CSS & JS · Live preview

What's included

Features

Uses crypto.getRandomValues exclusively — never Math.random for token bytes
Selectable token length: 16, 32, or 48 random bytes
Hex and base64url output encodings, switchable without regenerating manually
Monospace readonly-style display box for the generated token
Clipboard copy via navigator.clipboard.writeText with button feedback
One-click regenerate button for a fresh secure value
Live entropy readout in bits based on selected byte length
Zero dependencies — pure Web Crypto and DOM APIs

About this UI Snippet

Secure Token Generator — Web Crypto Random API Keys

Screenshot of the Secure Token Generator snippet rendered live

This snippet generates API tokens and secret keys suitable for real use, because every byte comes from crypto.getRandomValues, the browser's cryptographically secure pseudo-random number generator, never from Math.random.

Random bytes to text

A Uint8Array of the selected length is filled in place by crypto.getRandomValues(bytes). From there, bytesToHex() converts each byte to a two-character hex pair, while bytesToBase64Url() builds a binary string and runs it through btoa, then swaps the standard base64 alphabet's +// characters and strips padding to produce a URL-safe token.

Length and encoding controls

A row of pill buttons lets the user pick 16, 32, or 48 bytes (128/256/384 bits of entropy), and a second row switches between hex and base64url encoding. Both controls immediately regenerate the token so the displayed value always matches the selected settings.

Copy and regenerate

The Copy button calls navigator.clipboard.writeText and shows temporary "Copied!" feedback, while Regenerate simply reruns the same generation function to produce a fresh value on demand.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Hand this snippet's HTML, CSS, and JavaScript to an AI coding assistant like Claude and ask it to adapt "Secure Token Generator" to your project — restyle it to match your design system, wire it up to real data instead of the static example, or port it to the framework you're building in.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a forms component called "Secure Token Generator" using plain HTML, CSS, and vanilla JavaScript — no framework, no build step.

Requirements:
- Match the structure and behavior of the "Secure Token Generator" snippet from the UI Snippets Library.
- Keep the markup semantic and the styling self-contained (no external dependencies beyond what the original snippet uses).
- Keep the JavaScript vanilla, with no framework runtime required.
- Make it easy to restyle via CSS custom properties or class overrides so it can be dropped into a real project.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="stg-card">
  <div class="stg-head">
    <h2>Secure Token Generator</h2>
    <p>Cryptographically secure API keys generated with the Web Crypto API.</p>
  </div>

  <div class="stg-field">
    <label>Token length (bytes)</label>
    <div class="stg-lengths" id="stgLengths">
      <button class="stg-len-btn" data-len="16">16</button>
      <button class="stg-len-btn active" data-len="32">32</button>
      <button class="stg-len-btn" data-len="48">48</button>
    </div>
  </div>

  <div class="stg-field">
    <label>Encoding</label>
    <div class="stg-lengths" id="stgFormats">
      <button class="stg-len-btn active" data-fmt="hex">Hex</button>
      <button class="stg-len-btn" data-fmt="base64url">Base64url</button>
    </div>
  </div>

  <div class="stg-token-box">
    <code id="stgTokenOutput">generating…</code>
  </div>

  <div class="stg-actions">
    <button class="stg-btn stg-btn-primary" id="stgCopyBtn" type="button">Copy</button>
    <button class="stg-btn" id="stgRegenBtn" type="button">Regenerate</button>
  </div>

  <div class="stg-meta" id="stgMeta">32 bytes · 256 bits of entropy</div>
</div>

Step by step

How to Use

  1. 1
    Load the snippetClick "Secure Token Generator" in the sidebar to load its HTML, CSS, and JS into the editor panels. The preview updates instantly.
  2. 2
    Edit the codeModify any panel — HTML, CSS, or JS. The preview refreshes as you type. Use Reset in each panel header to restore the original.
  3. 3
    Preview on devicesClick the Mobile (375px), Tablet (768px), or Desktop buttons in the preview header to check responsiveness.
  4. 4
    Export in your formatClick "HTML" to download a standalone file, "JSX" for a React component, "Tailwind" for a React + Tailwind CSS component, "Tailwind HTML" for a standalone HTML file with Tailwind CDN, "Vue" for a Vue 3 SFC with <template>/<script setup>/<style scoped>, or "Angular" for a standalone Angular .component.ts file. "Copy all" copies the full code to clipboard.
  5. 5
    Save your versionClick "Save as", type a name, and press Enter. Your snippet saves to IndexedDB and appears in the Saved tab.

Real-world uses

Common Use Cases

API key generation UIs
Let users generate a new API key or secret directly in a dashboard.
Developer tool sites
Offer a quick, trustworthy token generator utility page.
Onboarding and setup flows
Generate a webhook signing secret or client secret during app setup.
Web Crypto API teaching example
Demonstrates secure random byte generation and encoding conversions.

Got questions?

Frequently Asked Questions

Yes. Every byte comes from crypto.getRandomValues, backed by the operating system's CSPRNG, unlike Math.random which is not suitable for secrets.

Base64url is standard base64 with + and / replaced by - and _ and padding removed, making the token safe to use directly in URLs and filenames.

Entropy in bits equals byte length times 8, so a 32-byte token has 256 bits of entropy, far beyond what is brute-forceable.