> ## Documentation Index
> Fetch the complete documentation index at: https://docsuncv2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# crypt.generatebytes

Generates `size` random bytes, and returns them encoded in base64.

```luau theme={null}
crypt.generatebytes(size: number): string
```

The returned string's length is `4 * math.ceil(size / 3)` - implying `crypt.generatebytes(10)` returns a 16-character string.

Most commonly used to generate IVs, salts, nonces, and request tokens.

## Parameters

| Parameter | Type     | Description                                             |
| --------- | -------- | ------------------------------------------------------- |
| `size`    | `number` | Number of raw bytes to generate before base64-encoding. |

## Returns

`string` - sequence of random bytes, encoded in base64.

## Example

Pull 16 random bytes for use as a session ID, request <abbr title="A one-time random number, never re-used. Stops people copying and re-sending a request.">nonce</abbr>, or <abbr title="Random noise added before scrambling. Stops two identical things from looking identical after.">salt</abbr>.

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local generatebytes = crypt.generatebytes(10)
  =======
  -- crypt.generatebytes(size: number): string
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  local nonce = crypt.generatebytes(16)
  print(nonce, #nonce) --> "h0Ip3K8Q3nVfx7rRqRfM9w==" 24
  ```
</CodeGroup>

<Tip>
  Typically used to generate secure IVs or temporary nonces.
</Tip>
