# Hash

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

# Hash

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

class Hash
```

[Defined in src/crypto/include/icy/crypto/hash.h:38](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L38)

Incremental cryptographic hash engine wrapping OpenSSL EVP digest functions.

Construct with an algorithm name recognized by OpenSSL (e.g. "sha256", "md5"). Feed data with one or more calls to [update()](#update-13), then call [digest()](#digest) or [digestStr()](#digeststr) to finalize and retrieve the result. Call [reset()](#reset-22) to reuse the engine for a new computation without reallocating the context.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Hash`](#hash-4)  | Constructs a [Hash](#hash-3) engine for the given algorithm. |
|  | [`~Hash`](#hash-5)  | Destroys the [Hash](#hash-3) engine and releases OpenSSL resources. |
| `void` | [`update`](#update-13)  | Feeds a single character into the digest computation. |
| `void` | [`update`](#update-14)  | Feeds a string view into the digest computation. |
| `void` | [`update`](#update-15)  | Feeds a raw memory buffer into the digest computation. |
| const [`ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec) & | [`digest`](#digest)  | Finalizes the digest computation and returns the raw binary result. |
| `std::string` | [`digestStr`](#digeststr)  | Finalizes the digest computation and returns the result as a raw binary string (not hex-encoded). Use [icy::hex::encode()](https://auscaster.com/icey/docs/api-reference/icy-hex.md#encode-14) on [digest()](#digest) if you need a printable representation. |
| `void` | [`reset`](#reset-22)  | Resets the digest context and clears the cached result, allowing the engine to be reused for a new computation with the same algorithm. |
| `const std::string &` | [`algorithm`](#algorithm) `const` | Returns the algorithm name this engine was constructed with. |

---

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

### Hash

```cpp
Hash(const std::string & algorithm)
```

[Defined in src/crypto/include/icy/crypto/hash.h:45](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L45)

Constructs a [Hash](#hash-3) engine for the given algorithm.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `algorithm` | `const std::string &` | OpenSSL digest name (e.g. "sha256", "sha1", "md5"). |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::runtime_error` | if the algorithm is not recognized by OpenSSL. |

---

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

### ~Hash

```cpp
~Hash()
```

[Defined in src/crypto/include/icy/crypto/hash.h:48](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L48)

Destroys the [Hash](#hash-3) engine and releases OpenSSL resources.

---

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

### update

```cpp
void update(char data)
```

[Defined in src/crypto/include/icy/crypto/hash.h:53](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L53)

Feeds a single character into the digest computation.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `data` | `char` | The byte to hash. |

---

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

### update

```cpp
void update(std::string_view data)
```

[Defined in src/crypto/include/icy/crypto/hash.h:58](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L58)

Feeds a string view into the digest computation.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `data` | `std::string_view` | The data to hash. |

---

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

### update

```cpp
void update(const void * data, size_t length)
```

[Defined in src/crypto/include/icy/crypto/hash.h:66](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L66)

Feeds a raw memory buffer into the digest computation.

This method may be called multiple times for streaming large inputs.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `data` | `const void *` | Pointer to the input buffer. |
| `length` | `size_t` | Number of bytes to hash from `data`. |

---

<a id="digest"></a>

### digest

```cpp
const ByteVec & digest()
```

[Defined in src/crypto/include/icy/crypto/hash.h:75](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L75)

Finalizes the digest computation and returns the raw binary result.

The result is computed on the first call and cached; subsequent calls return the same value without recomputing. Call [reset()](#reset-22) before reusing the engine for a new computation.

#### Returns
Reference to the internal byte vector containing the digest.

---

<a id="digeststr"></a>

### digestStr

```cpp
std::string digestStr()
```

[Defined in src/crypto/include/icy/crypto/hash.h:82](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L82)

Finalizes the digest computation and returns the result as a raw binary string (not hex-encoded). Use [icy::hex::encode()](https://auscaster.com/icey/docs/api-reference/icy-hex.md#encode-14) on [digest()](#digest) if you need a printable representation.

#### Returns
Binary digest as a std::string.

---

<a id="reset-22"></a>

### reset

```cpp
void reset()
```

[Defined in src/crypto/include/icy/crypto/hash.h:86](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L86)

Resets the digest context and clears the cached result, allowing the engine to be reused for a new computation with the same algorithm.

---

<a id="algorithm"></a>

### algorithm

`const`

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

[Defined in src/crypto/include/icy/crypto/hash.h:91](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L91)

Returns the algorithm name this engine was constructed with.

#### Returns
OpenSSL digest name string (e.g. "sha256").

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| [`EvpMdCtxPtr`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#evpmdctxptr) | [`_ctx`](#_ctx-1)  |  |
| `const EVP_MD *` | [`_md`](#_md)  |  |
| [`crypto::ByteVec`](https://auscaster.com/icey/docs/api-reference/icy-crypto.md#bytevec) | [`_digest`](#_digest)  |  |
| `std::string` | [`_algorithm`](#_algorithm)  |  |

---

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

### _ctx

```cpp
EvpMdCtxPtr _ctx
```

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

[Defined in src/crypto/include/icy/crypto/hash.h:96](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L96)

---

<a id="_md"></a>

### _md

```cpp
const EVP_MD * _md
```

[Defined in src/crypto/include/icy/crypto/hash.h:97](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L97)

---

<a id="_digest"></a>

### _digest

```cpp
crypto::ByteVec _digest
```

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

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

---

<a id="_algorithm"></a>

### _algorithm

```cpp
std::string _algorithm
```

[Defined in src/crypto/include/icy/crypto/hash.h:99](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L99)

## Protected Methods

| Return | Name | Description |
|--------|------|-------------|
| [`Hash`](#hash-3) & | [`operator=`](#operator-240)  |  |

---

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

### operator=

```cpp
Hash & operator=(Hash const &)
```

[Defined in src/crypto/include/icy/crypto/hash.h:94](https://github.com/auscaster/icey/blob/main/src/crypto/include/icy/crypto/hash.h#L94)
