Choosing an alphabet

  • Alphanumeric — safe in URLs, filenames and database collations.
  • Lowercase hex — compact, easy to compare, no case ambiguity.
  • Base64url — dense random tokens for URLs and headers.
  • Full ASCII — maximum entropy per character, but escaping headaches.

Tokens are not keys

A random string is not automatically a secure token. Security depends on entropy and on how the value is stored and compared. A 16-character alphanumeric string has about 95 bits; that is plenty for a session identifier, but only if the server compares it in constant time and stores it hashed.

For API keys and session tokens, generate at least 128 bits of entropy and treat the value as a secret. Never log it in plaintext.

Test data and collisions

Random strings in fixtures make tests non-deterministic. If a test asserts on the value, seed the generator or hard-code the string. If the test only needs uniqueness, random is fine.