# Cipher

<a id="cipher"></a>

# Cipher

```cpp
#include <icy/crypto/cipher.h>

class Cipher
```

[Defined in src/crypto/include/icy/crypto/cipher.h:40](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L40)

Provides symmetric algorithms for encryption and decryption. The algorithms that are available depend on the particular version of OpenSSL that is installed.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Cipher`](#cipher-1)  | Constructs a [Cipher](#cipher) with a randomly generated key and IV. |
|  | [`Cipher`](#cipher-2)  | Constructs a [Cipher](#cipher) with an explicit key and initialization vector. |
|  | [`Cipher`](#cipher-3)  | Constructs a [Cipher](#cipher) and derives a key and IV from a passphrase. |
|  | [`~Cipher`](#cipher-4)  | Destroys the [Cipher](#cipher) and resets the OpenSSL context. |
| `void` | [`initEncryptor`](#initencryptor)  | Initializes the cipher context for encryption. |
| `void` | [`initDecryptor`](#initdecryptor)  | Initializes the cipher context for decryption. |
| `ssize_t` | [`update`](#update-11)  | Processes a block of data through the cipher (encrypt or decrypt). |
| `ssize_t` | [`update`](#update-12) `inline` | Processes a block of data through the cipher using generic buffer types. |
| `ssize_t` | [`final`](#final)  | Finalizes the cipher operation and flushes any remaining buffered data. |
| `ssize_t` | [`final`](#final-1) `inline` | Finalizes the cipher operation using a generic output buffer type. |
| `ssize_t` | [`encrypt`](#encrypt)  | Encrypts a buffer and writes the result with optional transport encoding. |
| `ssize_t` | [`encrypt`](#encrypt-1) `inline` | Encrypts data using generic input/output buffer types. |
| `std::string` | [`encryptString`](#encryptstring-1) `virtual` | Encrypts a string and returns the result with optional transport encoding. |
| `std::string` | [`decryptString`](#decryptstring-1) `virtual` | Decrypts a string that was previously encrypted with optional encoding. |
| `void` | [`encryptStream`](#encryptstream) `virtual` | Encrypts all data from `source` and writes the result to `sink`. |
| `void` | [`decryptStream`](#decryptstream) `virtual` | Decrypts all data from `source` and writes the result to `sink`. |
| `void` | [`setKey`](#setkey-1) `inline` | Sets the encryption key. |
| `void` | [`setIV`](#setiv) `inline` | Sets the initialization vector (IV). |
| `int` | [`setPadding`](#setpadding)  | Enables or disables PKCS block padding. |
| const [`ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec) & | [`getKey`](#getkey) `const` | Returns the raw encryption key bytes. |
| const [`ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec) & | [`getIV`](#getiv) `const` | Returns the raw initialization vector bytes. |
| `const std::string &` | [`name`](#name-15) `const` | Returns the OpenSSL cipher name this object was constructed with. |
| `int` | [`blockSize`](#blocksize) `const` | Returns the cipher block size in bytes. |
| `int` | [`keySize`](#keysize) `const` | Returns the required key length in bytes for this cipher. |
| `int` | [`ivSize`](#ivsize) `const` | Returns the required initialization vector length in bytes. |
| `const EVP_CIPHER *` | [`cipher`](#cipher-5)  | Returns the underlying OpenSSL EVP_CIPHER object. |

---

<a id="cipher-1"></a>

### Cipher

```cpp
Cipher(const std::string & name)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:47](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L47)

Constructs a [Cipher](#cipher) with a randomly generated key and IV.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `name` | `const std::string &` | OpenSSL cipher name (e.g. "aes-256-cbc"). |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::invalid_argument` | if the cipher name is not recognized. |

---

<a id="cipher-2"></a>

### Cipher

```cpp
Cipher(const std::string & name, const ByteVec & key, const ByteVec & iv)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:55](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L55)

Constructs a [Cipher](#cipher) with an explicit key and initialization vector.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `name` | `const std::string &` | OpenSSL cipher name (e.g. "aes-256-cbc"). |
| `key` | const [`ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec) & | Encryption key; must match the cipher's required key length. |
| `iv` | const [`ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec) & | Initialization vector; must match the cipher's IV length. |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::invalid_argument` | if the cipher name is not recognized. |

---

<a id="cipher-3"></a>

### Cipher

```cpp
Cipher(const std::string & name, std::string_view passphrase, std::string_view salt, int iterationCount)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:67](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L67)

Constructs a [Cipher](#cipher) and derives a key and IV from a passphrase.

Uses EVP_BytesToKey with SHA-256 to derive the key material.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `name` | `const std::string &` | OpenSSL cipher name (e.g. "aes-256-cbc"). |
| `passphrase` | `std::string_view` | Secret passphrase for key derivation. |
| `salt` | `std::string_view` | Optional salt string; empty string means no salt. Values longer than 8 bytes are folded via XOR. |
| `iterationCount` | `int` | Number of key-derivation iterations. |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::invalid_argument` | if the cipher name is not recognized. |

---

<a id="cipher-4"></a>

### ~Cipher

```cpp
~Cipher()
```

[Defined in src/crypto/include/icy/crypto/cipher.h:71](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L71)

Destroys the [Cipher](#cipher) and resets the OpenSSL context.

---

<a id="initencryptor"></a>

### initEncryptor

```cpp
void initEncryptor()
```

[Defined in src/crypto/include/icy/crypto/cipher.h:77](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L77)

Initializes the cipher context for encryption.

Must be called before using [update()](#update-11) and [final()](#final) in encrypt mode. Calling this resets any prior context state.

---

<a id="initdecryptor"></a>

### initDecryptor

```cpp
void initDecryptor()
```

[Defined in src/crypto/include/icy/crypto/cipher.h:83](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L83)

Initializes the cipher context for decryption.

Must be called before using [update()](#update-11) and [final()](#final) in decrypt mode. Calling this resets any prior context state.

---

<a id="update-11"></a>

### update

```cpp
ssize_t update(const unsigned char * input, size_t inputLength, unsigned char * output, size_t outputLength)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:98](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L98)

Processes a block of data through the cipher (encrypt or decrypt).

Hand consecutive blocks of data to this method for streaming operation. The output buffer must be at least `inputLength + blockSize() - 1` bytes. After all input is processed, call [final()](#final) to flush any remaining buffered data from the cipher context.

#### Returns
Number of bytes written to `output`.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `input` | `const unsigned char *` | Pointer to the input data buffer. |
| `inputLength` | `size_t` | Number of bytes to process from `input`. |
| `output` | `unsigned char *` | Pointer to the output buffer. |
| `outputLength` | `size_t` | Size of the output buffer in bytes. |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::runtime_error` | if the output buffer is too small. |

---

<a id="update-12"></a>

### update

`inline`

```cpp
template<typename I, typename O> inline ssize_t update(const I & input, O & output)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:110](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L110)

Processes a block of data through the cipher using generic buffer types.

Convenience wrapper around [update(const unsigned char*, size_t,
unsigned char*, size_t)](#update-11). Accepts any type supported by internal::Raw.

#### Returns
Number of bytes written to `output`.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `input` | `const I &` | Input buffer (std::string, ByteVec, etc.). |
| `output` | `O &` | Output buffer; must be large enough for the result. |

---

<a id="final"></a>

### final

```cpp
ssize_t final(unsigned char * output, size_t length)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:130](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L130)

Finalizes the cipher operation and flushes any remaining buffered data.

Must be called after the last [update()](#update-11) call to retrieve any trailing cipher block. Further calls to [update()](#update-11) or [final()](#final) after this point produce undefined results; call [initEncryptor()](#initencryptor) / [initDecryptor()](#initdecryptor) to reset. The output buffer must be at least [blockSize()](#blocksize) bytes.

See EVP_CipherFinal_ex for further information.

#### Returns
Number of bytes written to `output`.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `output` | `unsigned char *` | Pointer to the output buffer; must be at least [blockSize()](#blocksize) bytes. |
| `length` | `size_t` | Size of the output buffer in bytes. |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::runtime_error` | if the output buffer is smaller than [blockSize()](#blocksize). |

---

<a id="final-1"></a>

### final

`inline`

```cpp
template<typename O> inline ssize_t final(O & output)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:140](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L140)

Finalizes the cipher operation using a generic output buffer type.

Convenience wrapper around [final(unsigned char*, size_t)](#final). Accepts any type supported by internal::Raw.

#### Returns
Number of bytes written to `output`.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `output` | `O &` | Output buffer; must hold at least [blockSize()](#blocksize) bytes. |

---

<a id="encrypt"></a>

### encrypt

```cpp
ssize_t encrypt(const unsigned char * inbuf, size_t inlen, unsigned char * outbuf, size_t outlen, Encoding encoding = Binary)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:168](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L168)

Encrypts a buffer and writes the result with optional transport encoding.

Calls [initEncryptor()](#initencryptor), [update()](#update-11), and [final()](#final) internally; the cipher does not need to be pre-initialized. The output buffer must be large enough to hold the encrypted and encoded result.

#### Returns
Total number of bytes written to `outbuf`.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `inbuf` | `const unsigned char *` | Pointer to the plaintext input buffer. |
| `inlen` | `size_t` | Number of bytes to encrypt from `inbuf`. |
| `outbuf` | `unsigned char *` | Pointer to the output buffer. |
| `outlen` | `size_t` | Size of the output buffer in bytes. |
| `encoding` | [`Encoding`](#encoding-1) | Transport encoding applied to the ciphertext (default: Binary). |

---

<a id="encrypt-1"></a>

### encrypt

`inline`

```cpp
template<typename I, typename O> inline ssize_t encrypt(const I & input, O & output, Encoding encoding = Binary)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:183](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L183)

Encrypts data using generic input/output buffer types.

Convenience wrapper around [encrypt(const unsigned char*, size_t,
unsigned char*, size_t, Encoding)](#encrypt). Accepts any type supported by internal::Raw.

#### Returns
Total number of bytes written to `output`.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `input` | `const I &` | Input buffer containing plaintext. |
| `output` | `O &` | Output buffer; must be large enough for the result. |
| `encoding` | [`Encoding`](#encoding-1) | Transport encoding applied to the ciphertext (default: Binary). |

---

<a id="encryptstring-1"></a>

### encryptString

`virtual`

```cpp
virtual std::string encryptString(const std::string & str, Encoding encoding = Binary)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:198](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L198)

Encrypts a string and returns the result with optional transport encoding.

Internally streams through [encryptStream()](#encryptstream); the cipher is re-initialized on each call.

#### Returns
Encrypted (and optionally encoded) result as a std::string.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `str` | `const std::string &` | Plaintext string to encrypt. |
| `encoding` | [`Encoding`](#encoding-1) | Transport encoding for the output (default: Binary). |

---

<a id="decryptstring-1"></a>

### decryptString

`virtual`

```cpp
virtual std::string decryptString(const std::string & str, Encoding encoding = Binary)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:208](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L208)

Decrypts a string that was previously encrypted with optional encoding.

Internally streams through [decryptStream()](#decryptstream); the cipher is re-initialized on each call.

#### Returns
Decrypted plaintext as a std::string.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `str` | `const std::string &` | Ciphertext string to decrypt, in the format given by `encoding`. |
| `encoding` | [`Encoding`](#encoding-1) | Transport encoding of the input (default: Binary). |

---

<a id="encryptstream"></a>

### encryptStream

`virtual`

```cpp
virtual void encryptStream(std::istream & source, std::ostream & sink, Encoding encoding = Binary)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:218](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L218)

Encrypts all data from `source` and writes the result to `sink`.

Reads in chunks of `blockSize() * 128` bytes. Calls [initEncryptor()](#initencryptor) internally; no prior initialization is required.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `source` | `std::istream &` | Input stream containing plaintext. |
| `sink` | `std::ostream &` | Output stream to receive the encrypted (and encoded) data. |
| `encoding` | [`Encoding`](#encoding-1) | Transport encoding applied to the output (default: Binary). |

---

<a id="decryptstream"></a>

### decryptStream

`virtual`

```cpp
virtual void decryptStream(std::istream & source, std::ostream & sink, Encoding encoding = Binary)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:228](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L228)

Decrypts all data from `source` and writes the result to `sink`.

Reads in chunks of `blockSize() * 128` bytes. Calls [initDecryptor()](#initdecryptor) internally; no prior initialization is required.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `source` | `std::istream &` | Input stream containing ciphertext (in the given encoding). |
| `sink` | `std::ostream &` | Output stream to receive the decrypted plaintext. |
| `encoding` | [`Encoding`](#encoding-1) | Transport encoding of the input data (default: Binary). |

---

<a id="setkey-1"></a>

### setKey

`inline`

```cpp
template<typename T> inline void setKey(const T & key)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:235](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L235)

Sets the encryption key.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `key` | `const T &` | Container whose size must exactly match [keySize()](#keysize). |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::logic_error` | if key.size() != [keySize()](#keysize). |

---

<a id="setiv"></a>

### setIV

`inline`

```cpp
template<typename T> inline void setIV(const T & iv)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:250](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L250)

Sets the initialization vector (IV).

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `iv` | `const T &` | Container whose size must exactly match [ivSize()](#ivsize). |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::logic_error` | if iv.size() != [ivSize()](#ivsize). |

---

<a id="setpadding"></a>

### setPadding

```cpp
int setPadding(int padding)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:271](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L271)

Enables or disables PKCS block padding.

By default, encryption pads input to a block boundary and decryption strips and validates the padding. If `padding` is zero, no padding is applied; the total data length must then be an exact multiple of [blockSize()](#blocksize) or the operation will fail.

See EVP_CIPHER_CTX_set_padding for further information.

#### Returns
The return value from EVP_CIPHER_CTX_set_padding.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `padding` | `int` | Non-zero to enable padding (default), zero to disable. |

---

<a id="getkey"></a>

### getKey

`const`

```cpp
const ByteVec & getKey() const
```

[Defined in src/crypto/include/icy/crypto/cipher.h:276](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L276)

Returns the raw encryption key bytes.

#### Returns
Reference to the internal key byte vector.

---

<a id="getiv"></a>

### getIV

`const`

```cpp
const ByteVec & getIV() const
```

[Defined in src/crypto/include/icy/crypto/cipher.h:281](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L281)

Returns the raw initialization vector bytes.

#### Returns
Reference to the internal IV byte vector.

---

<a id="name-15"></a>

### name

`const`

```cpp
const std::string & name() const
```

[Defined in src/crypto/include/icy/crypto/cipher.h:286](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L286)

Returns the OpenSSL cipher name this object was constructed with.

#### Returns
[Cipher](#cipher) name string (e.g. "aes-256-cbc").

---

<a id="blocksize"></a>

### blockSize

`const`

```cpp
int blockSize() const
```

[Defined in src/crypto/include/icy/crypto/cipher.h:291](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L291)

Returns the cipher block size in bytes.

#### Returns
Block size as reported by EVP_CIPHER_block_size.

---

<a id="keysize"></a>

### keySize

`const`

```cpp
int keySize() const
```

[Defined in src/crypto/include/icy/crypto/cipher.h:296](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L296)

Returns the required key length in bytes for this cipher.

#### Returns
Key length as reported by EVP_CIPHER_key_length.

---

<a id="ivsize"></a>

### ivSize

`const`

```cpp
int ivSize() const
```

[Defined in src/crypto/include/icy/crypto/cipher.h:301](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L301)

Returns the required initialization vector length in bytes.

#### Returns
IV length as reported by EVP_CIPHER_iv_length.

---

<a id="cipher-5"></a>

### cipher

```cpp
const EVP_CIPHER * cipher()
```

[Defined in src/crypto/include/icy/crypto/cipher.h:307](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L307)

Returns the underlying OpenSSL EVP_CIPHER object.

#### Returns
Pointer to the OpenSSL cipher descriptor; valid for the lifetime of this [Cipher](#cipher) object.

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| `bool` | [`_initialized`](#_initialized-2)  |  |
| `bool` | [`_encrypt`](#_encrypt)  |  |
| `const EVP_CIPHER *` | [`_cipher`](#_cipher)  |  |
| [`EvpCipherCtxPtr`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#evpcipherctxptr) | [`_ctx`](#_ctx)  |  |
| `std::string` | [`_name`](#_name-4)  |  |
| [`ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec) | [`_key`](#_key-2)  |  |
| [`ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec) | [`_iv`](#_iv)  |  |

---

<a id="_initialized-2"></a>

### _initialized

```cpp
bool _initialized
```

[Defined in src/crypto/include/icy/crypto/cipher.h:337](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L337)

---

<a id="_encrypt"></a>

### _encrypt

```cpp
bool _encrypt
```

[Defined in src/crypto/include/icy/crypto/cipher.h:338](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L338)

---

<a id="_cipher"></a>

### _cipher

```cpp
const EVP_CIPHER * _cipher
```

[Defined in src/crypto/include/icy/crypto/cipher.h:339](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L339)

---

<a id="_ctx"></a>

### _ctx

```cpp
EvpCipherCtxPtr _ctx
```

Type: [`EvpCipherCtxPtr`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#evpcipherctxptr)

[Defined in src/crypto/include/icy/crypto/cipher.h:340](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L340)

---

<a id="_name-4"></a>

### _name

```cpp
std::string _name
```

[Defined in src/crypto/include/icy/crypto/cipher.h:341](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L341)

---

<a id="_key-2"></a>

### _key

```cpp
ByteVec _key
```

Type: [`ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec)

[Defined in src/crypto/include/icy/crypto/cipher.h:342](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L342)

---

<a id="_iv"></a>

### _iv

```cpp
ByteVec _iv
```

Type: [`ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec)

[Defined in src/crypto/include/icy/crypto/cipher.h:343](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L343)

## Protected Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Cipher`](#cipher-6)  | Deleted constructor. |
|  | [`Cipher`](#cipher-7)  | Deleted constructor. |
| [`Cipher`](#cipher) & | [`operator=`](#operator-238)  | Deleted assignment operator. |
|  | [`Cipher`](#cipher-8)  | Deleted constructor. |
| [`Cipher`](#cipher) & | [`operator=`](#operator-239)  | Deleted assignment operator. |
| `void` | [`generateKey`](#generatekey)  | Derives and sets the key and IV from a passphrase using EVP_BytesToKey. |
| `void` | [`setRandomKey`](#setrandomkey)  | Fills the key buffer with cryptographically random bytes. |
| `void` | [`setRandomIV`](#setrandomiv)  | Fills the IV buffer with cryptographically random bytes. |
| `void` | [`init`](#init-15)  | Initializes or resets the OpenSSL cipher context for the given direction. |

---

<a id="cipher-6"></a>

### Cipher

```cpp
Cipher() = delete
```

[Defined in src/crypto/include/icy/crypto/cipher.h:310](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L310)

Deleted constructor.

---

<a id="cipher-7"></a>

### Cipher

```cpp
Cipher(const Cipher &) = delete
```

[Defined in src/crypto/include/icy/crypto/cipher.h:311](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L311)

Deleted constructor.

---

<a id="operator-238"></a>

### operator=

```cpp
Cipher & operator=(const Cipher &) = delete
```

[Defined in src/crypto/include/icy/crypto/cipher.h:312](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L312)

Deleted assignment operator.

---

<a id="cipher-8"></a>

### Cipher

```cpp
Cipher(Cipher &&) = delete
```

[Defined in src/crypto/include/icy/crypto/cipher.h:313](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L313)

Deleted constructor.

---

<a id="operator-239"></a>

### operator=

```cpp
Cipher & operator=(Cipher &&) = delete
```

[Defined in src/crypto/include/icy/crypto/cipher.h:314](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L314)

Deleted assignment operator.

---

<a id="generatekey"></a>

### generateKey

```cpp
void generateKey(std::string_view passphrase, std::string_view salt, int iterationCount)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:324](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L324)

Derives and sets the key and IV from a passphrase using EVP_BytesToKey.

Uses SHA-256 as the digest. Salt values longer than 8 bytes are folded by XOR into an 8-byte array as required by OpenSSL.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `passphrase` | `std::string_view` | Secret passphrase for key derivation. |
| `salt` | `std::string_view` | Salt string (may be empty for no salt). |
| `iterationCount` | `int` | Number of digest iterations. |

---

<a id="setrandomkey"></a>

### setRandomKey

```cpp
void setRandomKey()
```

[Defined in src/crypto/include/icy/crypto/cipher.h:327](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L327)

Fills the key buffer with cryptographically random bytes.

---

<a id="setrandomiv"></a>

### setRandomIV

```cpp
void setRandomIV()
```

[Defined in src/crypto/include/icy/crypto/cipher.h:330](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L330)

Fills the IV buffer with cryptographically random bytes.

---

<a id="init-15"></a>

### init

```cpp
void init(bool encrypt)
```

[Defined in src/crypto/include/icy/crypto/cipher.h:335](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L335)

Initializes or resets the OpenSSL cipher context for the given direction.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `encrypt` | `bool` | true to initialize for encryption, false for decryption. |

## Public Types

| Name | Description |
|------|-------------|
| [`Encoding`](#encoding-1)  | Transport encoding to use for [encrypt()](#encrypt) and decrypt(). |

---

<a id="encoding-1"></a>

### Encoding

```cpp
enum Encoding
```

[Defined in src/crypto/include/icy/crypto/cipher.h:147](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/cipher.h#L147)

Transport encoding to use for [encrypt()](#encrypt) and decrypt().

| Value | Description |
|-------|-------------|
| `Binary` | Plain binary output. |
| `Base64` | Base64-encoded output. |
| `BinHex` | BinHex-encoded output. |
| `Base64_NoLF` | Base64-encoded output, no linefeeds. |
| `BinHex_NoLF` | BinHex-encoded output, no linefeeds. |
