# encodeString (Shared)

> Encodes or encrypts a text with an algorithm: TEA, AES-128, RSA, Base64, Base32 or zlib.

- **Platform**: MY Multiplayer (MYMP)
- **Kind**: Lua Function
- **Side**: Shared
- **Category**: Data and utilities
- **URL**: https://wiki.mymp.gg/en/encodeString

## Syntax

```lua
string|bool encodeString(string algorithm, string data [, table options = nil, function callback = nil])
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `algorithm` | `string` | `"tea"`, `"aes128"`, `"rsa"`, `"base64"`, `"base32"` or `"zlib"`. |
| `data` | `string` | The text to encode. |
| `options` | `table` | A table of options: `key`, `variant` or, for zlib, `format` (`"gzip"` by default, `"zlib"`, `"raw"` or a windowBits), `compression` (0 to 9, 9 by default) and `strategy` (`"default"`, `"filtered"`, `"huffman"`, `"rle"`, `"fixed"`). *(default: `nil`)* |
| `callback` | `function` | If given, the work runs in the background and the function gets the result (or `false`) when it is done. *(default: `nil`)* |

## Returns

The result (with `aes128`, the cipher text and the 16-byte initialisation vector `decodeString` needs). With `callback`, `true` right away. `false` if the options are invalid or the operation failed.

### Algorithms

`options` is required for `"tea"`, `"aes128"` and `"rsa"`.

| Algorithm  | Options                                                                                                                     |
| ---------- | --------------------------------------------------------------------------------------------------------------------------- |
| `"tea"`    | Key in `key`; only its first 16 bytes count.                                                                                |
| `"aes128"` | AES-128 in CTR mode; `key` must be exactly 16 bytes long.                                                                   |
| `"rsa"`    | `key` holds the public key to encode and the private key to decode.                                                         |
| `"base64"` | Optional `variant = "URL"` for the URL-safe alphabet.                                                                       |
| `"base32"` | Optional `variant = "HEX"` for the hexadecimal alphabet.                                                                    |
| `"zlib"`   | Optional `format` (`"gzip"` default, `"zlib"`, `"raw"` or windowBits), `compression` (`0`–`9`, default `9`) and `strategy`. |

## Example

```lua
local key = "0123456789abcdef"
local cipher, iv = encodeString("aes128", "secret message", { key = key })
local plain = decodeString("aes128", cipher, { key = key, iv = iv })
```

## See also

- [decodeString (Shared)](https://wiki.mymp.gg/en/decodeString.md): Decodes or decrypts a text encodeString produced.
- [hash (Shared)](https://wiki.mymp.gg/en/hash.md): Hashes a text: MD5, SHA-1, SHA-2 or an HMAC.
