randbytes()
The randbytes() function returns a binary value filled with random bytes of the specified length.
Syntax
randbytes(LENGTH)
Parameters
LENGTH- An integer constant specifying the length of the binary to generate. Must be a value between 1 and 10,240 inclusive.
Description
The randbytes() function uses Java's Random class to generate a binary value filled with random bytes of the specified length.
LENGTH must be an integer constant evaluated at parse time. An error is raised at query parse time if the value is not an integer, is less than 1, or exceeds 10,240.
The randbytes() function returns a different binary value on each call.
Error codes
| Error code | Description |
|---|---|
| 90760 | LENGTH is not an integer. |
| 90761 | LENGTH is less than 1 or exceeds 10,240. |
Usage examples
-
Generate 32 bytes of random binary
json "{}" | eval rand_key = randbytes(32) | # rand_key: (hexadecimal representation of a 32-byte random binary) -
Use random bytes as an encryption key
json "{}" | eval key = randbytes(16), data = encode("hello, world!"), encrypted = encrypt("AES", key, data)
Compatibility
The randbytes() function has been available since before Sonar 4.0.