RANDSTR Expression
RANDSTR generates a cryptographically secure random string directly inside an ACE expression.
Syntax
mode is optional. When omitted, ACE uses ALNUM.
The requested length must be from 1 to 1024 characters.
Modes
| Mode | Characters | Example use |
|---|---|---|
ALNUM |
A-Z, a-z, 0-9 |
General-purpose random identifiers and tokens |
NUMERIC |
0-9 |
Numeric-only codes |
ALPHA |
A-Z, a-z |
Letter-only random strings |
HEX |
0-9, a-f |
Hexadecimal random values |
Mode names are case-insensitive. ALNUM is the default.
Examples
RANDSTR{32} and RANDSTR{32;ALNUM} use the same character set and generation path. As with any random generation, two calls can theoretically produce the same value; RANDSTR is not a uniqueness guarantee.
Security
The production implementation is explicit about the security properties used here:
- it uses OpenSSL
RAND_priv_bytes; - character selection uses rejection sampling rather than direct modulo mapping, avoiding modulo bias for the supported character-set sizes;
- if
RAND_priv_bytesreports failure, ACE throws an error instead of falling back toRANDOM,rand(), a timestamp, or another weaker generator.
RANDSTR provides cryptographic unpredictability, but random generation alone does not guarantee uniqueness. If a value must also be unique in a persistent namespace, enforce uniqueness where it is stored.
Validation
ACE rejects RANDSTR when:
lengthis missing, zero, non-numeric, or greater than 1024;- more than one optional parameter is supplied;
modeis missing after;or is not one ofALNUM,NUMERIC,ALPHA, orHEX.
Examples of invalid expressions already covered by the RANDSTR tests include:
RANDSTR vs. RANDOM
Use RANDOM{low,high} for general-purpose numeric random choices. Use RANDSTR{length[;mode]} when you need a cryptographically secure unpredictable string.