fourdst_plugin 0.0.1a
C++ Plugin Manager
Loading...
Searching...
No Matches
fourdst::crypt::PublicKey Class Reference

Represents a public key for cryptographic operations. More...

#include <public_key.h>

Public Member Functions

 PublicKey ()=default
 Constructs an uninitialized PublicKey object.
 
 PublicKey (const std::string &filepath)
 Constructs a PublicKey by loading from a file.
 
 PublicKey (const std::filesystem::path &filepath)
 Constructs a PublicKey by loading from a file.
 
 PublicKey (const char *filepath)
 Constructs a PublicKey by loading from a file.
 
 PublicKey (const std::vector< unsigned char > &data)
 Constructs a PublicKey from binary data in memory.
 
 ~PublicKey ()=default
 
 PublicKey (const PublicKey &)=delete
 
PublicKeyoperator= (const PublicKey &)=delete
 
 PublicKey (PublicKey &&)=default
 
PublicKeyoperator= (PublicKey &&) noexcept=default
 
std::string type () const
 Gets the type of the public key.
 
size_t size () const
 Gets the size of the public key in bits.
 
EVP_PKEY * get () const
 Gets the underlying OpenSSL EVP_PKEY object.
 
std::string get_fingerprint () const
 Gets a fingerprint of the public key.
 
bool is_initialized () const
 Checks if the PublicKey is initialized with a valid key.
 
bool operator== (const PublicKey &other) const
 Compares two PublicKey objects for equality.
 
bool operator!= (const PublicKey &other) const
 Compares two PublicKey objects for inequality.
 
bool operator== (const std::vector< unsigned char > &data) const
 Compares the PublicKey with raw key data for equality.
 
bool operator!= (const std::vector< unsigned char > &data) const
 Compares the PublicKey with raw key data for inequality.
 

Private Attributes

Unique_EVP_PKEY m_pkey
 The underlying OpenSSL key object.
 
bool m_initialized = false
 Flag indicating if the key is initialized.
 

Friends

std::ostream & operator<< (std::ostream &os, const PublicKey &key)
 Outputs a string representation of the PublicKey to a stream.
 

Detailed Description

Represents a public key for cryptographic operations.

This class provides an interface for loading and working with public keys in various formats (PEM, DER) and performing key-related operations.

Note
This class is move-constructible and move-assignable, but not copyable.

Constructor & Destructor Documentation

◆ PublicKey() [1/7]

fourdst::crypt::PublicKey::PublicKey ( )
default

Constructs an uninitialized PublicKey object.

◆ PublicKey() [2/7]

fourdst::crypt::PublicKey::PublicKey ( const std::string & filepath)
explicit

Constructs a PublicKey by loading from a file.

Parameters
[in]filepathPath to the file containing the public key in PEM format.
Exceptions
std::runtime_errorIf the file cannot be opened or the key cannot be loaded.

◆ PublicKey() [3/7]

fourdst::crypt::PublicKey::PublicKey ( const std::filesystem::path & filepath)
explicit

Constructs a PublicKey by loading from a file.

Parameters
[in]filepathFilesystem path to the file containing the public key in PEM format.
Exceptions
std::runtime_errorIf the file cannot be opened or the key cannot be loaded.

◆ PublicKey() [4/7]

fourdst::crypt::PublicKey::PublicKey ( const char * filepath)
explicit

Constructs a PublicKey by loading from a file.

Parameters
[in]filepathC-style string path to the file containing the public key in PEM format.
Exceptions
std::runtime_errorIf the file cannot be opened or the key cannot be loaded.

◆ PublicKey() [5/7]

fourdst::crypt::PublicKey::PublicKey ( const std::vector< unsigned char > & data)
explicit

Constructs a PublicKey from binary data in memory.

The data can be in either PEM or DER format. The format is automatically detected.

Parameters
[in]dataBinary data containing the public key.
Exceptions
std::runtime_errorIf the key cannot be loaded from the provided data.
std::runtime_errorIf the key format is not recognized.

◆ ~PublicKey()

fourdst::crypt::PublicKey::~PublicKey ( )
default

◆ PublicKey() [6/7]

fourdst::crypt::PublicKey::PublicKey ( const PublicKey & )
delete

◆ PublicKey() [7/7]

fourdst::crypt::PublicKey::PublicKey ( PublicKey && )
default

Member Function Documentation

◆ get()

EVP_PKEY * fourdst::crypt::PublicKey::get ( ) const
nodiscard

Gets the underlying OpenSSL EVP_PKEY object.

Returns
EVP_PKEY* A pointer to the OpenSSL EVP_PKEY, or nullptr if not initialized.
Warning
The returned pointer is owned by this PublicKey object and should not be freed.

◆ get_fingerprint()

std::string fourdst::crypt::PublicKey::get_fingerprint ( ) const
nodiscard

Gets a fingerprint of the public key.

The fingerprint is a SHA-256 hash of the key's DER-encoded SubjectPublicKeyInfo.

Returns
std::string A string in the format "sha256:<hex_encoded_hash>".
Exceptions
std::runtime_errorIf the key is not initialized or an error occurs.

◆ is_initialized()

bool fourdst::crypt::PublicKey::is_initialized ( ) const
nodiscard

Checks if the PublicKey is initialized with a valid key.

Returns
bool True if the key is initialized, false otherwise.

◆ operator!=() [1/2]

bool fourdst::crypt::PublicKey::operator!= ( const PublicKey & other) const

Compares two PublicKey objects for inequality.

Parameters
[in]otherThe PublicKey to compare with.
Returns
bool True if the keys are not equal, false otherwise.

◆ operator!=() [2/2]

bool fourdst::crypt::PublicKey::operator!= ( const std::vector< unsigned char > & data) const

Compares the PublicKey with raw key data for inequality.

Parameters
[in]dataThe raw key data to compare with.
Returns
bool True if the keys are not equal, false otherwise.

◆ operator=() [1/2]

PublicKey & fourdst::crypt::PublicKey::operator= ( const PublicKey & )
delete

◆ operator=() [2/2]

PublicKey & fourdst::crypt::PublicKey::operator= ( PublicKey && )
defaultnoexcept

◆ operator==() [1/2]

bool fourdst::crypt::PublicKey::operator== ( const PublicKey & other) const

Compares two PublicKey objects for equality.

Parameters
[in]otherThe PublicKey to compare with.
Returns
bool True if the keys are equal, false otherwise.
Exceptions
std::runtime_errorIf an OpenSSL error occurs during comparison.

◆ operator==() [2/2]

bool fourdst::crypt::PublicKey::operator== ( const std::vector< unsigned char > & data) const

Compares the PublicKey with raw key data for equality.

Parameters
[in]dataThe raw key data to compare with.
Returns
bool True if the keys are equal, false otherwise.

◆ size()

size_t fourdst::crypt::PublicKey::size ( ) const
nodiscard

Gets the size of the public key in bits.

Returns
size_t The size of the key in bits, or 0 if the key is not initialized.

◆ type()

std::string fourdst::crypt::PublicKey::type ( ) const
nodiscard

Gets the type of the public key.

Returns
std::string A string representing the key type (e.g., "RSA", "EC"), or "Invalid" if the key is not initialized.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const PublicKey & key )
friend

Outputs a string representation of the PublicKey to a stream.

Parameters
[in,out]osThe output stream to write to.
[in]keyThe PublicKey to output.
Returns
std::ostream& Reference to the output stream.

Member Data Documentation

◆ m_initialized

bool fourdst::crypt::PublicKey::m_initialized = false
private

Flag indicating if the key is initialized.

◆ m_pkey

Unique_EVP_PKEY fourdst::crypt::PublicKey::m_pkey
private

The underlying OpenSSL key object.


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