Skip to main content
This is a simulation/stub implementation. Generated keys are dummy bytes (zero-filled), not real cryptographic material. Do not use in production security contexts.

Data types

KeyType enum

Identifies the algorithm associated with a stored key.
ValueDescription
KeyType::RSARSA asymmetric key
KeyType::AESAES symmetric key

Key struct

id
std::string
required
Unique identifier for the key, e.g. "RSA_2048" or "AES_256".
type
KeyType
required
Algorithm type: KeyType::RSA or KeyType::AES.
data
std::vector<uint8_t>
required
Raw key bytes. In this stub implementation, always zero-filled with length equal to bits / 8.

Methods

void init()

Initializes the HSM instance. Prints "HSM initialized" to standard output. Must be called before any other method. Parameters: None Returns: void
HSM hsm;
hsm.init();
// Output: HSM initialized

std::string generate_rsa_key(int bits = 2048)

Generates a stub RSA key entry and stores it in the internal key map.
bits
int
default:"2048"
Key size in bits. Controls the length of the zero-filled dummy byte vector stored (bits / 8 bytes).
Returns: std::string — the key ID in the format "RSA_<bits>", e.g. "RSA_2048".
HSM hsm;
hsm.init();
std::string key_id = hsm.generate_rsa_key(2048);
// key_id == "RSA_2048"

std::string key_id_4096 = hsm.generate_rsa_key(4096);
// key_id_4096 == "RSA_4096"

std::string generate_aes_key(int bits = 256)

Generates a stub AES key entry and stores it in the internal key map.
bits
int
default:"256"
Key size in bits. Controls the length of the zero-filled dummy byte vector stored (bits / 8 bytes). Common values: 128, 192, 256.
Returns: std::string — the key ID in the format "AES_<bits>", e.g. "AES_256".
HSM hsm;
hsm.init();
std::string key_id = hsm.generate_aes_key(256);
// key_id == "AES_256"

std::string key_id_128 = hsm.generate_aes_key(128);
// key_id_128 == "AES_128"

Key get_key(const std::string& id)

Retrieves a previously generated key by its ID.
id
std::string
required
The key ID returned by generate_rsa_key() or generate_aes_key(), e.g. "RSA_2048".
Returns: Key — the matching Key struct. Returns an empty default-constructed Key{} (empty id, empty data) if the ID is not found.
HSM hsm;
hsm.init();
std::string key_id = hsm.generate_rsa_key(2048);

Key k = hsm.get_key(key_id);
// k.id   == "RSA_2048"
// k.type == KeyType::RSA
// k.data.size() == 256  (2048 / 8)

Key missing = hsm.get_key("nonexistent");
// missing.id   == ""
// missing.data == {}

std::vector<uint8_t> get_random_bytes(size_t length)

Generates cryptographically seeded random bytes using std::random_device.
length
size_t
required
Number of random bytes to generate.
Returns: std::vector<uint8_t> — a vector of length random bytes.
HSM hsm;
hsm.init();
std::vector<uint8_t> rand_bytes = hsm.get_random_bytes(32);
// rand_bytes.size() == 32
// Each byte is a random value from std::random_device

Complete example

#include "hsm.h"
#include <iostream>

int main() {
    HSM hsm;
    hsm.init();

    // Generate keys
    std::string rsa_id = hsm.generate_rsa_key(2048);
    std::string aes_id = hsm.generate_aes_key(256);

    // Retrieve and inspect
    Key rsa_key = hsm.get_key(rsa_id);
    std::cout << "RSA key id: " << rsa_key.id << "\n";
    std::cout << "RSA key bytes: " << rsa_key.data.size() << "\n";

    Key aes_key = hsm.get_key(aes_id);
    std::cout << "AES key id: " << aes_key.id << "\n";

    // Generate random bytes
    auto rand = hsm.get_random_bytes(16);
    std::cout << "Random bytes count: " << rand.size() << "\n";

    return 0;
}

Build docs developers (and LLMs) love