StormByte-Crypto 2.0.0
C++26 cryptography module of the StormByte suite
 
Loading...
Searching...
No Matches
generic.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Crypto.
5 *
6 * StormByte-Crypto original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Crypto source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, in particular Crypto++, bundled libbzip2, the StormByte-Buffer
23 * tree and the StormByte suite it vendors), which remains under its own license.
24 *
25 * Neither license grants any patent rights. Any patent licenses required
26 * to use this software or third-party components must be obtained separately
27 * from the patent holders.
28 *
29 * StormByte-Crypto is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * version 3 along with StormByte-Crypto. If not, see
36 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 *
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
39 */
40
41#pragma once
42
48
49#include <filesystem>
50#include <optional>
51#include <string_view>
52
57namespace StormByte {
62 namespace Crypto {
67 namespace KeyPair {
72 enum class Type {
73 DSA,
74 ECC,
75 ECDH,
76 ECDSA,
77 ED25519,
78 RSA,
79 X25519
80 };
81
86 enum class StorageFormat {
87 PEM,
88 DER
89 };
90
100 public:
109 Generic(const Generic& other) = default;
110
115 Generic(Generic&& other) noexcept = default;
116
120 virtual ~Generic() noexcept;
121
127 Generic& operator=(const Generic& other) = default;
128
134 Generic& operator=(Generic&& other) noexcept = default;
141 inline enum Type Type() const noexcept {
142 return m_type;
143 }
144
149 inline const StormByte::Safe::String& PublicKey() const noexcept {
150 return m_public_key;
151 }
152
157 inline bool HasPrivateKey() const noexcept {
158 return m_private_key.has_value();
159 }
160
165 inline const std::optional<Secure::Password>& PrivateKey() const noexcept {
166 return m_private_key;
167 }
168
180 bool Save(const std::filesystem::path& directory, std::string_view baseName, StorageFormat format = StorageFormat::PEM) const noexcept;
181
190 bool Save(const std::filesystem::path& directory, std::string_view baseName, const Secure::Password& encryptPassword, StorageFormat format = StorageFormat::PEM) const noexcept;
191
198 bool SavePublic(const std::filesystem::path& filePath, StorageFormat format = StorageFormat::PEM) const noexcept;
199
206 bool SavePrivate(const std::filesystem::path& filePath, StorageFormat format = StorageFormat::PEM) const noexcept;
207
215 bool SavePrivate(const std::filesystem::path& filePath, const Secure::Password& encryptPassword, StorageFormat format = StorageFormat::PEM) const noexcept;
218 protected:
221 std::optional<Secure::Password> m_private_key;
222
229 inline Generic(enum Type type, StormByte::Safe::String public_key, std::optional<Secure::Password> private_key = std::nullopt):
230 m_type(type),
231 m_public_key(std::move(public_key)),
232 m_private_key(std::move(private_key)) {}
233 };
234
241 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Create(Type type, unsigned short bits) noexcept;
242
249 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Load(const std::filesystem::path& publicKeyPath, const std::filesystem::path& privateKeyPath) noexcept;
250
258 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Load(const std::filesystem::path& publicKeyPath, const std::filesystem::path& privateKeyPath, const Secure::Password& password) noexcept;
259
265 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Load(const std::filesystem::path& path) noexcept;
266
273 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Load(const std::filesystem::path& path, const Secure::Password& password) noexcept;
274 }
275 }
276}
DSA keypair.
Definition dsa.hxx:69
ECC keypair.
Definition ecc.hxx:69
ECDH keypair.
Definition ecdh.hxx:69
ECDSA keypair.
Definition ecdsa.hxx:69
Ed25519 keypair.
Definition ed25519.hxx:69
Abstract keypair.
Definition generic.hxx:99
bool SavePrivate(const std::filesystem::path &filePath, StorageFormat format=StorageFormat::PEM) const noexcept
Write only the private key, unencrypted.
enum Type m_type
Algorithm.
Definition generic.hxx:219
const std::optional< Secure::Password > & PrivateKey() const noexcept
Private key, if any.
Definition generic.hxx:165
Generic(Generic &&other) noexcept=default
Move constructor.
bool Save(const std::filesystem::path &directory, std::string_view baseName, StorageFormat format=StorageFormat::PEM) const noexcept
Write public and private files under a directory.
Generic(enum Type type, StormByte::Safe::String public_key, std::optional< Secure::Password > private_key=std::nullopt)
Construct with algorithm and material.
Definition generic.hxx:229
std::optional< Secure::Password > m_private_key
Private material, if any.
Definition generic.hxx:221
bool HasPrivateKey() const noexcept
Whether a private key is stored.
Definition generic.hxx:157
const StormByte::Safe::String & PublicKey() const noexcept
Public key string.
Definition generic.hxx:149
Generic(const Generic &other)=default
Copy constructor.
virtual ~Generic() noexcept
Destructor.
StormByte::Safe::String m_public_key
Public material.
Definition generic.hxx:220
bool SavePrivate(const std::filesystem::path &filePath, const Secure::Password &encryptPassword, StorageFormat format=StorageFormat::PEM) const noexcept
Write only the private key, encrypted (prefer PEM).
bool Save(const std::filesystem::path &directory, std::string_view baseName, const Secure::Password &encryptPassword, StorageFormat format=StorageFormat::PEM) const noexcept
Write public and private files.
bool SavePublic(const std::filesystem::path &filePath, StorageFormat format=StorageFormat::PEM) const noexcept
Write only the public key.
RSA keypair.
Definition rsa.hxx:69
X25519 keypair.
Definition x25519.hxx:69
Shared, wiped container for passwords and raw key material.
Definition password.hxx:104
StorageFormat
On-disk encoding.
Definition generic.hxx:86
Generic::PointerType Create(Type type, unsigned short bits) noexcept
Generate a keypair.
Generic::PointerType Load(const std::filesystem::path &publicKeyPath, const std::filesystem::path &privateKeyPath) noexcept
Load from separate public and private files.
Root namespace of the StormByte suite.
STL namespace.
#define STORMBYTE_CRYPTO_PUBLIC
Definition visibility.h:53