C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
asymmetric_key.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../abi/string.hpp"
26#include "../compat.hpp"
27#include "../zstring_view.hpp"
28#include "common_types.hpp"
29
30#include <cstddef>
31#include <memory>
32#include <span>
33#include <string_view>
34
35namespace essence::crypto {
41 public:
47 ES_API(CPPESSENCE) explicit asymmetric_key(void* blob);
48
56 ES_API(CPPESSENCE)
57 asymmetric_key(use_public_tag tag, zstring_view path, const password_request_handler& handler = {});
58
65 ES_API(CPPESSENCE)
67 use_public_tag tag, std::span<const std::byte> buffer, const password_request_handler& handler = {});
68
76 ES_API(CPPESSENCE)
77 asymmetric_key(use_private_tag tag, zstring_view path, const password_request_handler& handler = {});
78
85 ES_API(CPPESSENCE)
87 use_private_tag tag, std::span<const std::byte> buffer, const password_request_handler& handler = {});
88
89 ES_API(CPPESSENCE) asymmetric_key(asymmetric_key&&) noexcept;
90 ES_API(CPPESSENCE) ~asymmetric_key();
91 ES_API(CPPESSENCE) asymmetric_key& operator=(asymmetric_key&&) noexcept;
92
98 [[nodiscard]] ES_API(CPPESSENCE) asymmetric_key_type type() const;
99
104 [[nodiscard]] ES_API(CPPESSENCE) abi::string name() const;
105
110 [[nodiscard]] ES_API(CPPESSENCE) abi::string description() const;
111
116 [[nodiscard]] ES_API(CPPESSENCE) void* to_blob() const noexcept;
117
122 [[nodiscard]] ES_API(CPPESSENCE) abi::string save_public() const;
123
128 ES_API(CPPESSENCE) void save_public(zstring_view path) const;
129
134 [[nodiscard]] ES_API(CPPESSENCE) abi::string save_private() const;
135
142 [[nodiscard]] ES_API(CPPESSENCE) abi::string
143 save_private(zstring_view cipher_name, std::string_view password) const;
144
149 ES_API(CPPESSENCE) void save_private(zstring_view path) const;
150
157 ES_API(CPPESSENCE)
158 void save_private(zstring_view path, zstring_view cipher_name, std::string_view password) const;
159
160 private:
161 class impl;
162
163 std::unique_ptr<impl> impl_;
164 };
165
179 template <pubkey_keygen_param T>
180 asymmetric_key generate_asymmetric_key_pair(const T& param) {
181 return asymmetric_key{param.generate_key_blob()};
182 }
183} // namespace essence::crypto
Definition zstring_view.hpp:34
Represents an asymmetric key or key pair for a pubkey encryption algorithm.
Definition asymmetric_key.hpp:40
asymmetric_key(use_public_tag tag, zstring_view path, const password_request_handler &handler={})
Creates an instance from a public key stored on the disk.
ES_API(CPPESSENCE) explicit asymmetric_key(void *blob)
Creates an instance from an internal blob.
void save_private(zstring_view path, zstring_view cipher_name, std::string_view password) const
Encrypts and saves the private key to a file.
Definition common_types.hpp:126
Definition common_types.hpp:33
Definition common_types.hpp:32