Crypto module

crypto

Cryptographic primitives, key helpers, and certificate utilities backed by OpenSSL.

Classes

NameDescription
CipherProvides symmetric algorithms for encryption and decryption. The algorithms that are available depend on the particular version of OpenSSL that is installed.
HashIncremental cryptographic hash engine wrapping OpenSSL EVP digest functions.
X509CertificateRAII wrapper for an OpenSSL X509 certificate with PEM loading and inspection.

Typedefs

ReturnNameDescription
std::unique_ptr< EVP_CIPHER_CTX, decltype(&EVP_CIPHER_CTX_free)>EvpCipherCtxPtrOwning OpenSSL cipher context handle with automatic EVP_CIPHER_CTX_free.
std::vector< unsigned char >ByteVecGeneric storage container for storing cryptographic binary data.
std::unique_ptr< EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>EvpMdCtxPtrOwning OpenSSL digest context handle with automatic EVP_MD_CTX_free.
::RSARSAKeyAlias for the OpenSSL RSA key type, brought into the icy::crypto namespace.
std::unique_ptr< X509, decltype(&X509_free)>X509PtrRAII pointer alias for OpenSSL X509* values.

EvpCipherCtxPtr

using EvpCipherCtxPtr = std::unique_ptr< EVP_CIPHER_CTX, decltype(&EVP_CIPHER_CTX_free)>

Defined in src/crypto/include/icy/crypto/cipher.h:34

Owning OpenSSL cipher context handle with automatic EVP_CIPHER_CTX_free.


ByteVec

using ByteVec = std::vector< unsigned char >

Defined in src/crypto/include/icy/crypto/crypto.h:63

Generic storage container for storing cryptographic binary data.


EvpMdCtxPtr

using EvpMdCtxPtr = std::unique_ptr< EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>

Defined in src/crypto/include/icy/crypto/hash.h:29

Owning OpenSSL digest context handle with automatic EVP_MD_CTX_free.


RSAKey

using RSAKey = ::RSA

Defined in src/crypto/include/icy/crypto/rsa.h:29

Alias for the OpenSSL RSA key type, brought into the icy::crypto namespace.

Currently a transparent alias for the OpenSSL RSA struct. Use OpenSSL RSA_* functions directly to create, populate, and free RSAKey objects. This alias exists as a stable forward-declaration point; a higher-level RAII wrapper may replace it in a future version.


X509Ptr

using X509Ptr = std::unique_ptr< X509, decltype(&X509_free)>

Defined in src/crypto/include/icy/crypto/x509certificate.h:30

RAII pointer alias for OpenSSL X509* values.

Functions

ReturnNameDescription
std::stringencryptStringEncrypts a string using the specified cipher, key, and IV in a single call.
std::stringdecryptStringDecrypts a string using the specified cipher, key, and IV in a single call.
voidinitializeEngineInitialize the Crypto library, as well as the underlying OpenSSL libraries.
voiduninitializeEngineUninitializes the Crypto library.
std::stringhash inlineComputes a hex-encoded digest of a string in a single call.
std::stringhash inlineComputes a hex-encoded digest of a raw buffer in a single call.
std::stringchecksum inlineComputes the hex-encoded checksum of a file using the given algorithm.
std::stringcomputeHMACComputes an HMAC-SHA1 message authentication code.

encryptString

template<typename K, typename I> std::string encryptString(const std::string & algorithm, const std::string & data, const K & key, const I & iv, Cipher::Encoding encoding = Cipher::Binary)

Defined in src/crypto/include/icy/crypto/cipher.h:361

Encrypts a string using the specified cipher, key, and IV in a single call.

Constructs a Cipher, optionally applies key and iv (skipped when empty), then delegates to Cipher::encryptString().

Returns

Encrypted (and optionally encoded) result as a std::string.

Parameters

ParameterTypeDescription
algorithmconst std::string &OpenSSL cipher name (e.g. "aes-256-cbc").
dataconst std::string &Plaintext string to encrypt.
keyconst K &Encryption key; pass an empty container to use a random key.
ivconst I &Initialization vector; pass an empty container to use a random IV.
encodingCipher::EncodingTransport encoding for the output (default: Binary).

decryptString

template<typename K, typename I> std::string decryptString(const std::string & algorithm, const std::string & data, const K & key, const I & iv, Cipher::Encoding encoding = Cipher::Binary)

Defined in src/crypto/include/icy/crypto/cipher.h:390

Decrypts a string using the specified cipher, key, and IV in a single call.

Constructs a Cipher, optionally applies key and iv (skipped when empty), then delegates to Cipher::decryptString().

Returns

Decrypted plaintext as a std::string.

Parameters

ParameterTypeDescription
algorithmconst std::string &OpenSSL cipher name (e.g. "aes-256-cbc").
dataconst std::string &Ciphertext string to decrypt, in the format given by encoding.
keyconst K &Decryption key; pass an empty container to use a random key.
ivconst I &Initialization vector; pass an empty container to use a random IV.
encodingCipher::EncodingTransport encoding of the input data (default: Binary).

initializeEngine

void initializeEngine()

Defined in src/crypto/include/icy/crypto/crypto.h:57

Initialize the Crypto library, as well as the underlying OpenSSL libraries.

OpenSSL must be initialized before using any classes from the Crypto library. OpenSSL will be initialized automatically through OpenSSL instances held by various Crypto classes (Cipher, Hash, X509Certificate), however it is recommended to call initializeEngine() in any case at application startup.

The Crypto library can be called multiple times; however, for every call to initializeEngine(), a matching call to uninitializeEngine() must be performed.


uninitializeEngine

void uninitializeEngine()

Defined in src/crypto/include/icy/crypto/crypto.h:60

Uninitializes the Crypto library.


hash

inline

inline std::string hash(const std::string & algorithm, std::string_view data)

Defined in src/crypto/include/icy/crypto/hash.h:108

Computes a hex-encoded digest of a string in a single call.

Returns

Lowercase hex-encoded digest string.

Parameters

ParameterTypeDescription
algorithmconst std::string &OpenSSL digest name (e.g. "sha256", "md5").
datastd::string_viewInput data to hash.

hash

inline

inline std::string hash(const std::string & algorithm, const void * data, unsigned length)

Defined in src/crypto/include/icy/crypto/hash.h:122

Computes a hex-encoded digest of a raw buffer in a single call.

Returns

Lowercase hex-encoded digest string.

Parameters

ParameterTypeDescription
algorithmconst std::string &OpenSSL digest name (e.g. "sha256", "md5").
dataconst void *Pointer to the input buffer.
lengthunsignedNumber of bytes to hash.

checksum

inline

inline std::string checksum(const std::string & algorithm, const std::string & path)

Defined in src/crypto/include/icy/crypto/hash.h:139

Computes the hex-encoded checksum of a file using the given algorithm.

Reads the file in 4096-byte chunks; suitable for large files.

Returns

Lowercase hex-encoded digest string.

Parameters

ParameterTypeDescription
algorithmconst std::string &OpenSSL digest name (e.g. "sha256", "md5").
pathconst std::string &Filesystem path to the file to hash.

Exceptions

ExceptionDescription
std::runtime_errorif the file cannot be opened.

computeHMAC

std::string computeHMAC(std::string_view input, std::string_view key)

Defined in src/crypto/include/icy/crypto/hmac.h:33

Computes an HMAC-SHA1 message authentication code.

Uses OpenSSL HMAC with SHA-1 as the underlying digest. The output is a 20-byte raw binary string (not hex-encoded).

Returns

20-byte raw binary HMAC-SHA1 digest.

Parameters

ParameterTypeDescription
inputstd::string_viewData to authenticate.
keystd::string_viewSecret key used for the HMAC computation.

Exceptions

ExceptionDescription
std::runtime_errorif OpenSSL returns an unexpected digest length.