Token generator

Random strings with the characters you choose.

loading Token generator
About this tool

What is a random token generator?

A token is a string of unpredictable characters that nobody should be able to guess: an API key, a session secret, a password-reset link, a webhook signing secret, a CSRF value, a database seed password. What makes it safe is not how it looks but where the randomness comes from. A token built with Math.random() or a timestamp can be reproduced by an attacker who knows roughly when it was made. A good one comes from a cryptographically secure random number generator (CSPRNG).

This generator draws every character with the browser's crypto.getRandomValues, the standard Web Crypto source meant for keys and secrets. It also avoids a subtle flaw that many snippets have, modulo bias: when you reduce a 32-bit random number with % 62, the first characters of the alphabet come up slightly more often than the last. Here numbers that fall in the uneven tail are thrown away and redrawn, so every character in your alphabet is exactly equally likely.

You choose the alphabet (uppercase, lowercase, digits, symbols), a length from 1 to 512 and how many tokens to make at once, from 1 to 100. A line under the result shows the strength in bits of entropy: the length multiplied by log2 of the alphabet size. 128 bits or more is the usual target for secrets that guard something real. Nothing is sent to a server; the page makes no request while you generate, so the tokens exist only in your tab. For passwords you will type by hand, see the password strength analyser; for identifiers rather than secrets, the UUID generator is the better fit.

How to generate a random token

  1. Open the page: it already shows a 64-character token made of uppercase letters, lowercase letters and digits. Copy it with Copy if that is what you need.
  2. Switch the character types on or off: Uppercase, Lowercase, Numbers and Symbols. When Symbols is on, a field lets you edit which symbols are allowed, which helps with systems that reject " or \.
  3. Turn on Skip look-alike characters if a person will read the token aloud or type it from paper. It removes 0, O, 1, l, I and |.
  4. To exclude other characters, type them in Characters to exclude, for example $& for a value that will go inside a shell command.
  5. Set the Length with the number box, the slider or one of the preset buttons (16, 32, 64, 128, 256, 512).
  6. Set Number of tokens to get a batch, one per line, for example to seed test accounts.
  7. Read the strength line, then press Refresh for new values or Copy to take them.

Examples

A 32-character API key

Uppercase, lowercase and digits give an alphabet of 62 characters. At length 32 the strength line reads 191 bits (32 × log2(62), rounded). The output has this shape, with different characters every time:

k3Vq9RzTn0LbX7yUe2WmHc5AdJf8PsGo

The default 64-character secret

Same alphabet, length 64: 381 bits. That is far beyond what is needed to resist guessing, which is why it works as a JWT signing secret or a session key. A hexadecimal secret of the same length carries only 256 bits, because each character holds just 4 bits.

A token people will type

Uppercase, lowercase and digits with Skip look-alike characters on leaves 57 characters. At length 24 the line reads 140 bits, and nobody has to decide whether a character is an l or a 1.

Why symbols are optional

Adding the default symbols raises the alphabet from 62 to 85 characters. At length 32 that gives 205 bits instead of 191. For machine-to-machine secrets, a few more characters of length buys the same strength without risking an escaping problem in a URL, a .env file or a YAML value.

A 6-digit code is only 20 bits

Digits only, length 6: 20 bits. It is fine for a one-time code that expires in minutes and is rate limited, and never fine as a long-lived secret.

Common errors and how to fix them

  • "There are no characters left to build a token. Turn on at least one character type, or remove the excluded characters.": every type is off, or the exclusion list removed the whole alphabet. Turn one type back on.
  • "The length must be a whole number from 1 to 512.": the length box has a decimal or a value out of range. Use a whole number or a preset.
  • "The number of tokens must be a whole number from 1 to 100.": the same rule for the batch size.
  • The service rejects my token: many APIs refuse certain characters. Turn Symbols off or edit the symbols list; letters and digits are accepted almost everywhere.

Frequently asked questions

Is this token generator secure?

Yes for the purpose it was made for. Characters come from the browser's cryptographic random source and are drawn without modulo bias. Everything happens locally, so the result is never transmitted. As with any secret, store it in a password manager or a secrets vault, not in a chat.

How long should a token be?

Aim for at least 128 bits of entropy, which is 22 characters from the 62-character alphabet, and use 32 or more to leave a margin. Session identifiers and API keys of 32 to 64 characters are common. For a secret that signs data, such as an HMAC key, match the size of the hash output.

What is the difference between a token and a password?

A token is made for machines and can be as long and random as you like. A password has to be remembered or typed by a person, so it trades length for usability. If you need to judge a human password, use the password strength analyser.

Generate a token in code

JavaScript

function token(length = 32, alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
  const limit = Math.floor(2 ** 32 / alphabet.length) * alphabet.length;
  const buf = new Uint32Array(1);
  let out = '';
  while (out.length < length) {
    crypto.getRandomValues(buf);
    if (buf[0] < limit) out += alphabet[buf[0] % alphabet.length];
  }
  return out;
}

Python

import secrets, string

alphabet = string.ascii_letters + string.digits
print(''.join(secrets.choice(alphabet) for _ in range(32)))

Go

// import "crypto/rand" and "encoding/base64"
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil { panic(err) }
fmt.Println(base64.RawURLEncoding.EncodeToString(b))

Bash

openssl rand -base64 48 | tr -d '\n=' | cut -c1-32
Report a problem