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 codeDescription
90760LENGTH is not an integer.
90761LENGTH is less than 1 or exceeds 10,240.

Usage examples

  1. Generate 32 bytes of random binary

    json "{}" | eval rand_key = randbytes(32)
    | # rand_key: (hexadecimal representation of a 32-byte random binary)
    
  2. 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.