# MessageIntegrity

<a id="messageintegrity"></a>

# MessageIntegrity

```cpp
#include <icy/stun/attributes.h>

class MessageIntegrity
```

[Defined in src/stun/include/icy/stun/attributes.h:468](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L468)

> **Inherits:** [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute)

Implements the STUN MESSAGE-INTEGRITY attribute (RFC 5389 section 15.4). On write, computes an HMAC-SHA1 over the message bytes preceding this attribute when a key is set. On read, captures the raw HMAC bytes and the input bytes needed to verify them later via [verifyHmac()](#verifyhmac).

## List of all members

| Name | Kind | Owner |
|------|------|-------|
| [`MessageIntegrity`](#messageintegrity-1) | `function` | Declared here |
| [`MessageIntegrity`](#messageintegrity-2) | `function` | Declared here |
| [`~MessageIntegrity`](#messageintegrity-3) | `function` | Declared here |
| [`clone`](#clone-16) | `function` | Declared here |
| [`verifyHmac`](#verifyhmac) | `function` | Declared here |
| [`input`](#input) | `function` | Declared here |
| [`hmac`](#hmac) | `function` | Declared here |
| [`key`](#key-2) | `function` | Declared here |
| [`setInput`](#setinput) | `function` | Declared here |
| [`setHmac`](#sethmac) | `function` | Declared here |
| [`setKey`](#setkey) | `function` | Declared here |
| [`read`](#read-11) | `function` | Declared here |
| [`write`](#write-40) | `function` | Declared here |
| [`TypeID`](#typeid-2) | `variable` | Declared here |
| [`Size`](#size-15) | `variable` | Declared here |
| [`_input`](#_input) | `variable` | Declared here |
| [`_hmac`](#_hmac) | `variable` | Declared here |
| [`_key`](#_key-1) | `variable` | Declared here |
| [`~Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute-1) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`clone`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#clone-12) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`read`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#read-7) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`write`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#write-36) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#type-17) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`size`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#size-12) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`paddingBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddingbytes) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`paddedBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddedbytes) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`consumePadding`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#consumepadding) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`writePadding`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#writepadding) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`typeString`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typestring) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`TypeID`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typeid) | `variable` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`create`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#create-10) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`paddingBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddingbytes-1) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`paddedBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddedbytes-1) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`typeString`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typestring-1) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`_type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#_type-1) | `variable` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`_size`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#_size-3) | `variable` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute-2) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`setLength`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#setlength) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`Type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#type-18) | `enum` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |

## Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute)

| Kind | Name | Description |
|------|------|-------------|
| `function` | [`~Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute-1) `virtual` `inline` |  |
| `function` | [`clone`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#clone-12) `virtual` | Returns a deep copy of this attribute. |
| `function` | [`read`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#read-7) `virtual` | Reads the body (not the type or size) for this type of attribute from the given buffer. |
| `function` | [`write`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#write-36) `virtual` `const` | Writes the body (not the type or size) to the given buffer. |
| `function` | [`type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#type-17) `const` |  |
| `function` | [`size`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#size-12) `const` |  |
| `function` | [`paddingBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddingbytes) `const` `inline` |  |
| `function` | [`paddedBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddedbytes) `const` `inline` |  |
| `function` | [`consumePadding`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#consumepadding) `const` | Advances the reader past any 4-byte alignment padding that follows this attribute's body. |
| `function` | [`writePadding`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#writepadding) `const` | Writes zero-fill padding bytes to align this attribute to a 4-byte boundary. |
| `function` | [`typeString`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typestring)  |  |
| `variable` | [`TypeID`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typeid) `static` `constexpr` |  |
| `function` | [`create`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#create-10) `static` | Creates an attribute of the given wire type and body size. Returns nullptr if the type is unknown or the size is invalid. |
| `function` | [`paddingBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddingbytes-1) `static` `inline` `constexpr` | Returns the 4-byte alignment padding required for a body of `size` bytes. |
| `function` | [`paddedBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddedbytes-1) `static` `inline` `constexpr` | Returns the body length including 4-byte alignment padding. |
| `function` | [`typeString`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typestring-1) `static` |  |
| `variable` | [`_type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#_type-1)  |  |
| `variable` | [`_size`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#_size-3)  |  |
| `function` | [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute-2)  |  |
| `function` | [`setLength`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#setlength)  | Updates the stored body length. |
| `enum` | [`Type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#type-18)  |  |

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`MessageIntegrity`](#messageintegrity-1)  |  |
|  | [`MessageIntegrity`](#messageintegrity-2)  |  |
|  | [`~MessageIntegrity`](#messageintegrity-3) `virtual` |  |
| std::unique_ptr< [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) > | [`clone`](#clone-16) `virtual` `override` | Returns a deep copy of this attribute. |
| `bool` | [`verifyHmac`](#verifyhmac) `const` | Verifies the stored HMAC against the stored input bytes using `key`. |
| `std::string` | [`input`](#input) `const` `inline` |  |
| `std::string` | [`hmac`](#hmac) `const` `inline` |  |
| `std::string` | [`key`](#key-2) `const` `inline` |  |
| `void` | [`setInput`](#setinput) `inline` | Sets the raw message bytes used as HMAC input during verification. |
| `void` | [`setHmac`](#sethmac) `inline` | Sets the raw HMAC value (used when copying a received attribute). |
| `void` | [`setKey`](#setkey) `inline` | Sets the HMAC key; triggers HMAC computation on [write()](#write-40). |
| `void` | [`read`](#read-11) `virtual` `override` | Reads the body (not the type or size) for this type of attribute from the given buffer. |
| `void` | [`write`](#write-40) `virtual` `const` `override` | Writes the body (not the type or size) to the given buffer. |

---

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

### MessageIntegrity

```cpp
MessageIntegrity()
```

[Defined in src/stun/include/icy/stun/attributes.h:471](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L471)

---

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

### MessageIntegrity

```cpp
MessageIntegrity(const MessageIntegrity & r)
```

[Defined in src/stun/include/icy/stun/attributes.h:472](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L472)

---

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

### ~MessageIntegrity

`virtual`

```cpp
virtual ~MessageIntegrity()
```

[Defined in src/stun/include/icy/stun/attributes.h:473](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L473)

---

<a id="clone-16"></a>

### clone

`virtual` `override`

```cpp
virtual std::unique_ptr< Attribute > clone() override
```

[Defined in src/stun/include/icy/stun/attributes.h:476](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L476)

Returns a deep copy of this attribute.

#### Reimplements

- [`Attribute::clone`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#clone-12)

---

<a id="verifyhmac"></a>

### verifyHmac

`const`

```cpp
bool verifyHmac(std::string_view key) const
```

[Defined in src/stun/include/icy/stun/attributes.h:484](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L484)

Verifies the stored HMAC against the stored input bytes using `key`. 
#### Returns
true if the computed HMAC matches the stored HMAC.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `key` | `std::string_view` | HMAC key (MD5 of username:realm:password for long-term creds). |

---

<a id="input"></a>

### input

`const` `inline`

```cpp
inline std::string input() const
```

[Defined in src/stun/include/icy/stun/attributes.h:487](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L487)

#### Returns
The raw message bytes captured at read time, used for HMAC verification.

---

<a id="hmac"></a>

### hmac

`const` `inline`

```cpp
inline std::string hmac() const
```

[Defined in src/stun/include/icy/stun/attributes.h:490](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L490)

#### Returns
The raw 20-byte HMAC value as read from the wire.

---

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

### key

`const` `inline`

```cpp
inline std::string key() const
```

[Defined in src/stun/include/icy/stun/attributes.h:493](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L493)

#### Returns
The HMAC key set for outgoing message signing (empty if not set).

---

<a id="setinput"></a>

### setInput

`inline`

```cpp
inline void setInput(const std::string & input)
```

[Defined in src/stun/include/icy/stun/attributes.h:497](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L497)

Sets the raw message bytes used as HMAC input during verification.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `input` | `const std::string &` | Byte string of the message up to this attribute. |

---

<a id="sethmac"></a>

### setHmac

`inline`

```cpp
inline void setHmac(const std::string & hmac)
```

[Defined in src/stun/include/icy/stun/attributes.h:501](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L501)

Sets the raw HMAC value (used when copying a received attribute).

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `hmac` | `const std::string &` | 20-byte HMAC string. |

---

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

### setKey

`inline`

```cpp
inline void setKey(const std::string & key)
```

[Defined in src/stun/include/icy/stun/attributes.h:505](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L505)

Sets the HMAC key; triggers HMAC computation on [write()](#write-40).

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `key` | `const std::string &` | MD5 digest of the long-term credential (username:realm:password). |

---

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

### read

`virtual` `override`

```cpp
virtual void read(BitReader & reader) override
```

[Defined in src/stun/include/icy/stun/attributes.h:507](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L507)

Reads the body (not the type or size) for this type of attribute from the given buffer.

#### Reimplements

- [`Attribute::read`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#read-7)

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `reader` | [`BitReader`](https://auscaster.com/icey/docs/api-reference/icy-BitReader.md#bitreader) & | Source bit reader positioned at the attribute body. |

---

<a id="write-40"></a>

### write

`virtual` `const` `override`

```cpp
virtual void write(BitWriter & writer) const override
```

[Defined in src/stun/include/icy/stun/attributes.h:508](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L508)

Writes the body (not the type or size) to the given buffer.

#### Reimplements

- [`Attribute::write`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#write-36)

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `writer` | [`BitWriter`](https://auscaster.com/icey/docs/api-reference/icy-BitWriter.md#bitwriter) & | Destination bit writer. |

## Public Static Attributes

| Return | Name | Description |
|--------|------|-------------|
| `constexpr uint16_t` | [`TypeID`](#typeid-2) `static` `constexpr` |  |
| `constexpr uint16_t` | [`Size`](#size-15) `static` `constexpr` | HMAC-SHA1 output is always 20 bytes. |

---

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

### TypeID

`static` `constexpr`

```cpp
constexpr uint16_t TypeID = 0x0008
```

[Defined in src/stun/include/icy/stun/attributes.h:478](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L478)

---

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

### Size

`static` `constexpr`

```cpp
constexpr uint16_t Size = 20
```

[Defined in src/stun/include/icy/stun/attributes.h:479](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L479)

HMAC-SHA1 output is always 20 bytes.

## Private Attributes

| Return | Name | Description |
|--------|------|-------------|
| `std::string` | [`_input`](#_input)  |  |
| `std::string` | [`_hmac`](#_hmac)  |  |
| `std::string` | [`_key`](#_key-1)  |  |

---

<a id="_input"></a>

### _input

```cpp
std::string _input
```

[Defined in src/stun/include/icy/stun/attributes.h:511](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L511)

---

<a id="_hmac"></a>

### _hmac

```cpp
std::string _hmac
```

[Defined in src/stun/include/icy/stun/attributes.h:512](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L512)

---

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

### _key

```cpp
std::string _key
```

[Defined in src/stun/include/icy/stun/attributes.h:513](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L513)
