userver: userver/crypto/private_key.hpp Source File
Loading...
Searching...
No Matches
private_key.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/crypto/private_key.hpp
4/// @brief @copybrief crypto::PrivateKey
5
6#include <memory>
7#include <string_view>
8
9#include <userver/crypto/basic_types.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace crypto {
14
15/// @ingroup userver_universal userver_containers
16///
17/// Loaded into memory private key
19 public:
20 using NativeType = EVP_PKEY;
21
22 PrivateKey() = default;
23
24 NativeType* GetNative() const noexcept { return pkey_.get(); }
25 explicit operator bool() const noexcept { return !!pkey_; }
26
27 /// Accepts a string that contains a private key and a password, checks the
28 /// key and password, loads it into OpenSSL structures and returns as a
29 /// PrivateKey variable.
30 ///
31 /// @throw crypto::KeyParseError if failed to load the key.
32 static PrivateKey LoadFromString(std::string_view key,
33 std::string_view password);
34
35 /// Accepts a string that contains a private key (not protected with
36 /// password), checks the key and password, loads it into OpenSSL structures
37 /// and returns as a PrivateKey variable.
38 ///
39 /// @throw crypto::KeyParseError if failed to load the key.
40 static PrivateKey LoadFromString(std::string_view key);
41
42 private:
43 explicit PrivateKey(std::shared_ptr<NativeType> pkey)
44 : pkey_(std::move(pkey)) {}
45
46 std::shared_ptr<NativeType> pkey_{};
47};
48
49} // namespace crypto
50
51USERVER_NAMESPACE_END