CppSecurity 1.1.2.0
C++ Security Library
Loading...
Searching...
No Matches
cipher.h
Go to the documentation of this file.
1
9#ifndef CPPSECURITY_CIPHER_H
10#define CPPSECURITY_CIPHER_H
11
12#include "password.h"
13
14#include <memory>
15
16#include <openssl/evp.h>
17
18namespace CppSecurity {
19
22{
23 AES128,
24 AES192,
25 AES256,
26 ARIA128,
27 ARIA192,
28 ARIA256,
32};
33
35
43class Cipher
44{
45public:
47
53
58 Cipher(std::string_view secret, CipherAlgorithm algorithm = CipherAlgorithm::AES256, size_t iterations = 1000) : Cipher(algorithm, iterations) { Initialize(secret); }
60
66 Cipher(std::string_view secret, std::string_view salt, CipherAlgorithm algorithm = CipherAlgorithm::AES256, size_t iterations = 1000) : Cipher(algorithm, iterations) { Initialize(secret, salt); }
67 Cipher(const Cipher&) = delete;
68 Cipher(Cipher&&) = default;
69 ~Cipher() = default;
70
71 Cipher& operator=(const Cipher&) = delete;
72 Cipher& operator=(Cipher&&) = default;
73
75 CipherAlgorithm algorithm() const noexcept { return _algorithm; }
77 const std::string& name() const noexcept { return _name; }
79 size_t iterations() const noexcept { return _iterations; }
80
82
85 static std::string GenerateSalt();
86
88
91 void Initialize(std::string_view secret);
93
97 void Initialize(std::string_view secret, std::string_view salt);
98
100
104 std::string Encrypt(std::string_view str);
105
107
111 std::string Decrypt(std::string_view str);
112
113private:
114 CipherAlgorithm _algorithm;
115 std::string _name{"<unknown>"};
116 const EVP_CIPHER* _cipher{nullptr};
117 size_t _iterations;
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;
122
124 void InitializeContext();
125};
126
130} // namespace CppSecurity
131
132#endif // CPPSECURITY_CIPHER_H
void Initialize(std::string_view secret)
Initialize the cipher with the given secret key.
Definition cipher.cpp:86
Cipher & operator=(const Cipher &)=delete
CipherAlgorithm algorithm() const noexcept
Get the cipher algorithm.
Definition cipher.h:75
const std::string & name() const noexcept
Get the cipher algorithm name.
Definition cipher.h:77
static std::string GenerateSalt()
Generate the unique secret salt.
Definition cipher.cpp:79
Cipher(Cipher &&)=default
Cipher & operator=(Cipher &&)=default
size_t iterations() const noexcept
Get the count of key hashing iterations.
Definition cipher.h:79
std::string Decrypt(std::string_view str)
Decrypt the given string.
Definition cipher.cpp:140
Cipher(std::string_view secret, CipherAlgorithm algorithm=CipherAlgorithm::AES256, size_t iterations=1000)
Initialize cipher with the given secret key and required algorithm.
Definition cipher.h:58
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.
Definition cipher.h:66
Cipher(const Cipher &)=delete
std::string Encrypt(std::string_view str)
Encrypt the given string.
Definition cipher.cpp:117
CipherAlgorithm
Cipher algorithm.
Definition cipher.h:22
Password string definition.