9#ifndef CPPSECURITY_CIPHER_H
10#define CPPSECURITY_CIPHER_H
16#include <openssl/evp.h>
77 const std::string&
name() const noexcept {
return _name; }
79 size_t iterations() const noexcept {
return _iterations; }
97 void Initialize(std::string_view secret, std::string_view salt);
104 std::string
Encrypt(std::string_view str);
111 std::string
Decrypt(std::string_view str);
115 std::string _name{
"<unknown>"};
116 const EVP_CIPHER* _cipher{
nullptr};
118 uint8_t _key[EVP_MAX_KEY_LENGTH];
119 uint8_t _iv[EVP_MAX_IV_LENGTH];
120 std::unique_ptr<EVP_CIPHER_CTX, void (*)(EVP_CIPHER_CTX *)> _encrypt;
121 std::unique_ptr<EVP_CIPHER_CTX, void (*)(EVP_CIPHER_CTX *)> _decrypt;
124 void InitializeContext();
void Initialize(std::string_view secret)
Initialize the cipher with the given secret key.
Cipher & operator=(const Cipher &)=delete
CipherAlgorithm algorithm() const noexcept
Get the cipher algorithm.
const std::string & name() const noexcept
Get the cipher algorithm name.
static std::string GenerateSalt()
Generate the unique secret salt.
Cipher(Cipher &&)=default
Cipher & operator=(Cipher &&)=default
size_t iterations() const noexcept
Get the count of key hashing iterations.
std::string Decrypt(std::string_view str)
Decrypt the given string.
Cipher(std::string_view secret, CipherAlgorithm algorithm=CipherAlgorithm::AES256, size_t iterations=1000)
Initialize cipher with the given secret key and required algorithm.
Cipher(std::string_view secret, std::string_view salt, CipherAlgorithm algorithm=CipherAlgorithm::AES256, size_t iterations=1000)
Initialize cipher with the given secret key, unique salt and required algorithm.
Cipher(const Cipher &)=delete
std::string Encrypt(std::string_view str)
Encrypt the given string.
CipherAlgorithm
Cipher algorithm.
Password string definition.