StormByte C++ Library: Crypto module 1.0.0
StormByte-Crypto is a StormByte library module for handling cryptographic operations
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
StormByte::Crypto::KeyPair Class Referencefinal

A class representing a public/private key pair. More...

#include <keypair.hxx>

Public Member Functions

 KeyPair (const std::string &pub, const std::string &priv) noexcept
 Constructs a KeyPair with a public and private key.
 
 KeyPair (const std::string &pub) noexcept
 Constructs a KeyPair with a public key only.
 
 KeyPair (std::string &&pub, std::string &&priv) noexcept
 Constructs a KeyPair with a public and private key (move version).
 
 KeyPair (std::string &&pub) noexcept
 Constructs a KeyPair with a public key only (move version).
 
 KeyPair (const KeyPair &other)=default
 Copy constructor.
 
 KeyPair (KeyPair &&other) noexcept=default
 Move constructor.
 
 ~KeyPair () noexcept=default
 Destructor.
 
KeyPairoperator= (const KeyPair &other)=default
 Copy assignment operator.
 
KeyPairoperator= (KeyPair &&other) noexcept=default
 Move assignment operator.
 
const std::string & PublicKey () const noexcept
 Returns the public key.
 
const std::optional< std::string > & PrivateKey () const noexcept
 Returns the private key.
 

Static Public Member Functions

static Expected< KeyPair, ExceptionGenerate (const Algorithm::Asymmetric &algo, const size_t &key_size) noexcept
 Generates a random key pair using the specified algorithm and key size.
 
static Expected< KeyPair, ExceptionGenerate (const Algorithm::Asymmetric &algo, const std::string &curve_name) noexcept
 Generates a random key pair using the specified algorithm and curve name.
 
static Expected< KeyPair, ExceptionGenerate (const Algorithm::Sign &algo, const size_t &key_size) noexcept
 Generates a random key pair for signing using the specified algorithm and key size.
 
static Expected< KeyPair, ExceptionGenerate (const Algorithm::Sign &algo, const std::string &curve_name) noexcept
 Generates a random key pair for signing using the specified algorithm and curve name.
 
static Expected< KeyPair, ExceptionGenerate (const Algorithm::SecretShare &algo, const std::string &curve_name) noexcept
 Generates a random key pair for secret-sharing using the specified algorithm and curve name.
 

Detailed Description

A class representing a public/private key pair.

A KeyPair can contain both a public key and a private key, or only a public key.

Constructor & Destructor Documentation

◆ KeyPair() [1/6]

StormByte::Crypto::KeyPair::KeyPair ( const std::string &  pub,
const std::string &  priv 
)
noexcept

Constructs a KeyPair with a public and private key.

This constructor creates a KeyPair that can be used for all cryptographic operations, including encryption, decryption, signing, and signature verification.

Parameters
pubThe public key.
privThe private key.

◆ KeyPair() [2/6]

StormByte::Crypto::KeyPair::KeyPair ( const std::string &  pub)
noexcept

Constructs a KeyPair with a public key only.

This constructor creates a KeyPair that can only be used for encryption and signature verification. Decryption and signing operations will not be available because the private key is not provided.

Parameters
pubThe public key.

◆ KeyPair() [3/6]

StormByte::Crypto::KeyPair::KeyPair ( std::string &&  pub,
std::string &&  priv 
)
noexcept

Constructs a KeyPair with a public and private key (move version).

This constructor moves the public and private keys into the KeyPair instance.

Parameters
pubThe public key (rvalue reference).
privThe private key (rvalue reference).

◆ KeyPair() [4/6]

StormByte::Crypto::KeyPair::KeyPair ( std::string &&  pub)
noexcept

Constructs a KeyPair with a public key only (move version).

This constructor moves the public key into the KeyPair instance. Decryption and signing operations will not be available because the private key is not provided.

Parameters
pubThe public key (rvalue reference).

◆ KeyPair() [5/6]

StormByte::Crypto::KeyPair::KeyPair ( const KeyPair other)
default

Copy constructor.

Creates a copy of the given KeyPair instance.

Parameters
otherThe KeyPair instance to copy.

◆ KeyPair() [6/6]

StormByte::Crypto::KeyPair::KeyPair ( KeyPair &&  other)
defaultnoexcept

Move constructor.

Moves the given KeyPair instance into the current instance.

Parameters
otherThe KeyPair instance to move.

◆ ~KeyPair()

StormByte::Crypto::KeyPair::~KeyPair ( )
defaultnoexcept

Destructor.

Cleans up the KeyPair instance. This includes releasing any resources associated with the public and private keys.

Member Function Documentation

◆ Generate() [1/5]

static Expected< KeyPair, Exception > StormByte::Crypto::KeyPair::Generate ( const Algorithm::Asymmetric &  algo,
const size_t &  key_size 
)
staticnoexcept

Generates a random key pair using the specified algorithm and key size.

This method generates both a public and private key, creating a KeyPair that can be used for all cryptographic operations.

Parameters
algoThe algorithm to use for key generation.
key_sizeThe size of the key to generate.
Returns
An Expected containing the generated KeyPair or an error.

◆ Generate() [2/5]

static Expected< KeyPair, Exception > StormByte::Crypto::KeyPair::Generate ( const Algorithm::Asymmetric &  algo,
const std::string &  curve_name 
)
staticnoexcept

Generates a random key pair using the specified algorithm and curve name.

This method generates both a public and private key, creating a KeyPair that can be used for all cryptographic operations.

Parameters
algoThe algorithm to use for key generation.
curve_nameThe name of the elliptic curve (e.g., "secp256r1").
Returns
An Expected containing the generated KeyPair or an error.

◆ Generate() [3/5]

static Expected< KeyPair, Exception > StormByte::Crypto::KeyPair::Generate ( const Algorithm::SecretShare &  algo,
const std::string &  curve_name 
)
staticnoexcept

Generates a random key pair for secret-sharing using the specified algorithm and curve name.

This method generates both a public and private key, creating a KeyPair that can be used for shared secret generation.

Parameters
algoThe secret-sharing algorithm to use.
curve_nameThe name of the elliptic curve (e.g., "secp256r1").
Returns
An Expected containing the generated KeyPair or an error.

◆ Generate() [4/5]

static Expected< KeyPair, Exception > StormByte::Crypto::KeyPair::Generate ( const Algorithm::Sign &  algo,
const size_t &  key_size 
)
staticnoexcept

Generates a random key pair for signing using the specified algorithm and key size.

This method generates both a public and private key, creating a KeyPair that can be used for signing and signature verification.

Parameters
algoThe signing algorithm to use.
key_sizeThe size of the key to generate.
Returns
An Expected containing the generated KeyPair or an error.

◆ Generate() [5/5]

static Expected< KeyPair, Exception > StormByte::Crypto::KeyPair::Generate ( const Algorithm::Sign &  algo,
const std::string &  curve_name 
)
staticnoexcept

Generates a random key pair for signing using the specified algorithm and curve name.

This method generates both a public and private key, creating a KeyPair that can be used for signing and signature verification.

Parameters
algoThe signing algorithm to use.
curve_nameThe name of the elliptic curve (e.g., "secp256r1").
Returns
An Expected containing the generated KeyPair or an error.

◆ operator=() [1/2]

KeyPair & StormByte::Crypto::KeyPair::operator= ( const KeyPair other)
default

Copy assignment operator.

Assigns the values from the given KeyPair instance to the current instance.

Parameters
otherThe KeyPair instance to copy.
Returns
A reference to the current instance.

◆ operator=() [2/2]

KeyPair & StormByte::Crypto::KeyPair::operator= ( KeyPair &&  other)
defaultnoexcept

Move assignment operator.

Moves the values from the given KeyPair instance to the current instance.

Parameters
otherThe KeyPair instance to move.
Returns
A reference to the current instance.

◆ PrivateKey()

const std::optional< std::string > & StormByte::Crypto::KeyPair::PrivateKey ( ) const
noexcept

Returns the private key.

The private key is optional in a KeyPair. If the private key is not available, the KeyPair can only be used for encryption and signature verification. Decryption and signing will not be possible.

Returns
The private key as an optional string. If the private key is not available, the optional will be empty.

◆ PublicKey()

const std::string & StormByte::Crypto::KeyPair::PublicKey ( ) const
noexcept

Returns the public key.

The public key is always available in a KeyPair and can be used for encryption and signature verification.

Returns
The public key as a string.

The documentation for this class was generated from the following file: