Zutily
Security7 min readPublished March 2, 2026

Random String Generation: Tokens & API Keys

Random strings power authentication tokens, API keys, and test data. Learn the best practices for generating secure, properly encoded random strings for your applications.

Why Developers Need Random String Generators

Random strings are ubiquitous in modern software development. Every API key, session token, CSRF token, nonce, webhook secret, and temporary password is fundamentally a random string with specific encoding and length requirements. Generating these strings correctly — with sufficient randomness and proper formatting — is critical for application security.

Many developers resort to ad-hoc methods: concatenating timestamps, using Math.random(), or manually typing characters. These approaches produce predictable, insecure strings that can be guessed or reproduced by attackers. Cryptographically secure random string generation eliminates these vulnerabilities entirely.

Beyond security, random strings are essential for testing. QA engineers need realistic test data — email addresses, order numbers, session IDs — in large quantities. Batch random string generation with prefix/suffix formatting produces test datasets in seconds that would take hours to create manually.

HEX vs. Base64 vs. Custom Character Sets

HEX encoding uses characters 0-9 and a-f, producing strings like 'a3f2b7c1e9d04556'. HEX is commonly used for color codes (#FF6B35), cryptographic values, Ethereum addresses, and any context where the string represents raw bytes. Each HEX character encodes 4 bits, so a 32-character HEX string represents 128 bits of randomness.

Base64 encoding uses A-Z, a-z, 0-9, +, and / (with = for padding), producing strings like 'dGhpcyBpcyBhIHRlc3Q='. Base64 is denser than HEX — each character encodes 6 bits — making it more compact. It is commonly used for JWT tokens, HTTP Basic Authentication, data URIs, and encoding binary data for transmission in text-based protocols.

Custom character sets let you define exactly which characters appear in the generated string. This is useful for generating strings that match specific format requirements — for example, alphanumeric-only API keys (no special characters), numeric OTP codes, or strings that avoid visually ambiguous characters (0/O, 1/l/I) for human-readable tokens.

Security Considerations for Token Generation

Always use a cryptographically secure random number generator (CSPRNG) for security tokens. In browsers, this is crypto.getRandomValues(). In Node.js, use crypto.randomBytes(). Never use Math.random(), which is a pseudo-random number generator (PRNG) designed for statistical simulations, not security.

Token length determines security level. A 128-bit (32 hex character / 22 base64 character) token provides 2^128 possible values — effectively unguessable. For most applications, 256 bits (64 hex / 43 base64 characters) provides a comfortable security margin. API keys for public-facing services should be at least 256 bits.

Consider token entropy separately from token length. A 64-character token from a 16-character alphabet (HEX) has 256 bits of entropy. A 64-character token from a 62-character alphanumeric alphabet has about 381 bits of entropy. The larger the character set, the more entropy per character.

For tokens that will be visible to users (API keys, verification codes), include a prefix to identify the token type: 'sk_live_', 'pk_test_', 'ghp_'. This helps developers and security teams quickly identify the purpose and sensitivity level of a token in logs, configuration files, and code.

Batch Generation for Testing and Development

Test data generation is one of the most practical uses of random string generators. When building or testing a feature that processes session tokens, you might need 50-100 realistic tokens with specific formats. Batch generation creates these instantly, eliminating the manual effort of creating test fixtures.

Prefixes and suffixes in batch-generated strings help create realistic test data. A batch of strings with prefix 'ord_' and a random alphanumeric body produces order IDs like 'ord_a7f2b3c1'. Similarly, prefix 'usr_' creates user IDs, 'tok_' creates tokens, and custom prefixes match your application's naming conventions.

For database seeding, batch-generate identifiers in the exact format your schema expects — UUIDs for PostgreSQL uuid columns, base64 tokens for varchar fields, or hex strings for binary fields. This ensures test data is compatible with production formats and constraints.

Generate Random Strings with Zutily

Zutily's Random String Generator supports custom character sets, HEX and Base64 encoding modes, adjustable string length, prefix/suffix formatting, and batch generation of up to 100 strings at once. All generation uses the Web Crypto API for cryptographic security.

Whether you need a single API key for a new service, a batch of session tokens for load testing, or formatted test identifiers for database seeding, Zutily generates them instantly in your browser with zero server involvement. Copy individual strings or export the entire batch with a single click.

Enjoyed this article?

Share it with your network

Try the Tools Mentioned

Free, instant, and private — right in your browser.