---
title: llarp::KeyManager
summary: KeyManager manages the cryptographic keys stored on disk for the local node. 

---

# llarp::KeyManager



[KeyManager]() manages the cryptographic keys stored on disk for the local node.  [More...](#detailed-description)


`#include <key_manager.hpp>`

## Public Functions

|                | Name           |
| -------------- | -------------- |
| bool | **[backupFileByMoving](Classes/structllarp_1_1KeyManager.md#function-backupfilebymoving)**(const fs::path & filepath)<br>Utility function to backup a file by moving it.  |
| | **[KeyManager](Classes/structllarp_1_1KeyManager.md#function-keymanager)**()<br>Constructor.  |
| bool | **[initialize](Classes/structllarp_1_1KeyManager.md#function-initialize)**(const [llarp::Config](Classes/structllarp_1_1Config.md) & config, bool genIfAbsent, bool isSNode)<br>Initializes keys using the provided config, loading from disk.  |
| bool | **[getRouterContact](Classes/structllarp_1_1KeyManager.md#function-getroutercontact)**([llarp::RouterContact](Classes/structllarp_1_1RouterContact.md) & rc) const<br>Obtain the self-signed [RouterContact]().  |
| bool | **[needBackup](Classes/structllarp_1_1KeyManager.md#function-needbackup)**() const<br>Return whether or not we need to backup keys as we load them.  |

## Public Attributes

|                | Name           |
| -------------- | -------------- |
| [llarp::SecretKey](Classes/structllarp_1_1SecretKey.md) | **[identityKey](Classes/structllarp_1_1KeyManager.md#variable-identitykey)**  |
| [llarp::SecretKey](Classes/structllarp_1_1SecretKey.md) | **[encryptionKey](Classes/structllarp_1_1KeyManager.md#variable-encryptionkey)**  |
| [llarp::SecretKey](Classes/structllarp_1_1SecretKey.md) | **[transportKey](Classes/structllarp_1_1KeyManager.md#variable-transportkey)**  |
| fs::path | **[m_rcPath](Classes/structllarp_1_1KeyManager.md#variable-m-rcpath)**  |
| fs::path | **[m_idKeyPath](Classes/structllarp_1_1KeyManager.md#variable-m-idkeypath)**  |
| fs::path | **[m_encKeyPath](Classes/structllarp_1_1KeyManager.md#variable-m-enckeypath)**  |
| fs::path | **[m_transportKeyPath](Classes/structllarp_1_1KeyManager.md#variable-m-transportkeypath)**  |

## Detailed Description

```cpp
struct llarp::KeyManager;
```

[KeyManager]() manages the cryptographic keys stored on disk for the local node. 

This includes private keys as well as the self-signed router contact file (e.g. "self.signed").

Keys are either read from disk if they exist and are valid (see below) or are generated and written to disk.

In addition, the [KeyManager](Classes/structllarp_1_1KeyManager.md) detects when the keys obsolete (e.g. as a result of a software upgrade) and backs up existing keys before writing out new ones. 

## Public Functions Documentation

### function backupFileByMoving

```cpp
static bool backupFileByMoving(
    const fs::path & filepath
)
```

Utility function to backup a file by moving it. 

**Parameters**: 

  * **filepath** is the name of the original file to backup. 


**Return**: true if the file could be moved or didn't exist, false otherwise 

Attempts to find a new filename based on the original that doesn't exist, then moves it. The pattern used is originalFile.N.bak where N is the lowest integer matching a filename that doesn't exist.


### function KeyManager

```cpp
KeyManager()
```

Constructor. 

### function initialize

```cpp
bool initialize(
    const llarp::Config & config,
    bool genIfAbsent,
    bool isSNode
)
```

Initializes keys using the provided config, loading from disk. 

**Parameters**: 

  * **config** should be a prepared config object 
  * **genIfAbsent** determines whether or not we will create files if they do not exist. 
  * **isSNode** 


**Return**: true on success, false otherwise 

NOTE: Must be called prior to obtaining any keys. NOTE: blocks on I/O


### function getRouterContact

```cpp
bool getRouterContact(
    llarp::RouterContact & rc
) const
```

Obtain the self-signed [RouterContact](). 

**Parameters**: 

  * **rc** (out) will be modified to contian the [RouterContact](Classes/structllarp_1_1RouterContact.md)


**Return**: true on success, false otherwise 

### function needBackup

```cpp
inline bool needBackup() const
```

Return whether or not we need to backup keys as we load them. 

## Public Attributes Documentation

### variable identityKey

```cpp
llarp::SecretKey identityKey;
```


### variable encryptionKey

```cpp
llarp::SecretKey encryptionKey;
```


### variable transportKey

```cpp
llarp::SecretKey transportKey;
```


### variable m_rcPath

```cpp
fs::path m_rcPath;
```


### variable m_idKeyPath

```cpp
fs::path m_idKeyPath;
```


### variable m_encKeyPath

```cpp
fs::path m_encKeyPath;
```


### variable m_transportKeyPath

```cpp
fs::path m_transportKeyPath;
```


-------------------------------

Updated on 2023-06-06 at 21:47:58 +0000