Generate raw random bytes for cryptography and testing
The random byte generator asks the browser's Web Crypto API for the number of bytes you request and prints them in the format you need. Unlike a random string, which picks from an alphabet, this gives you the underlying binary data directly, which is what encryption keys, HMAC secrets, salts and nonces actually are.
Common sizes
| Bytes | Bits | Typical use |
|---|---|---|
| 12 | 96 | AES-GCM nonce (IV) |
| 16 | 128 | AES-128 key, password salt, CSRF token |
| 32 | 256 | AES-256 key, HMAC-SHA256 secret, JWT HS256 signing key, app keys |
| 64 | 512 | HMAC-SHA512 secret |
Random byte generator output formats
The same 8 random bytes in each format:
hex 9f3a07c2e15b44d8
base64 nzoHwuFbRNg=
decimal 159 58 7 194 225 91 68 216
array [0x9f, 0x3a, 0x07, 0xc2, 0xe1, 0x5b, 0x44, 0xd8]
binary 10011111 00111010 00000111 11000010 11100001 01011011 01000100 11011000- Hex – two characters per byte; the most common way to store keys in environment variables and config files.
- Base64 – about 33% shorter than hex. Laravel's
APP_KEY, for example, is 32 random bytes encoded in Base64 with abase64:prefix. - Decimal – each byte as a number 0–255, handy for teaching or for languages without hex literals.
- Array – a comma-separated list of
0x..literals you can paste into C, Java, Rust, Go or JavaScript source code. - Binary – eight bits per byte, useful for learning bitwise operations.
Random bytes vs random strings
A random string limited to letters and digits carries about 5.95 bits per character, while a byte carries a full 8 bits. When a library asks for a "32-byte key", it means 32 bytes of raw binary, not 32 typed characters. Generating the bytes directly and then encoding them avoids that confusion: the hex or Base64 text is only a transport format, and your code decodes it back to bytes before use.
The spaced hex option inserts a space between bytes (9f 3a 07 c2), which is easier to read when you compare values with a hexdump or protocol documentation.
Security notes
- Bytes come from
crypto.getRandomValues, a cryptographically secure generator, and never leave your browser. - Treat generated keys like passwords: keep them out of source control and rotate them if they leak.
- Do not use a human-readable password as an encryption key. If you must derive a key from a password, use a key-derivation function such as PBKDF2, scrypt or Argon2.
To convert the result into another representation later, use hex to Base64 or Base64 to hex. For identifiers rather than secrets, a UUID is usually the more convenient format.