rippled
Loading...
Searching...
No Matches
SecretKey.h
1#ifndef XRPL_PROTOCOL_SECRETKEY_H_INCLUDED
2#define XRPL_PROTOCOL_SECRETKEY_H_INCLUDED
3
4#include <xrpl/basics/Buffer.h>
5#include <xrpl/basics/Slice.h>
6#include <xrpl/protocol/KeyType.h>
7#include <xrpl/protocol/PublicKey.h>
8#include <xrpl/protocol/Seed.h>
9#include <xrpl/protocol/tokens.h>
10
11#include <array>
12#include <cstring>
13#include <string>
14
15namespace ripple {
16
19{
20private:
22
23public:
25
26 SecretKey() = delete;
27 SecretKey(SecretKey const&) = default;
29 operator=(SecretKey const&) = default;
30
31 ~SecretKey();
32
34 SecretKey(Slice const& slice);
35
36 std::uint8_t const*
37 data() const
38 {
39 return buf_;
40 }
41
43 size() const
44 {
45 return sizeof(buf_);
46 }
47
54 to_string() const;
55
57 begin() const noexcept
58 {
59 return buf_;
60 }
61
63 cbegin() const noexcept
64 {
65 return buf_;
66 }
67
69 end() const noexcept
70 {
71 return buf_ + sizeof(buf_);
72 }
73
75 cend() const noexcept
76 {
77 return buf_ + sizeof(buf_);
78 }
79};
80
81inline bool
82operator==(SecretKey const& lhs, SecretKey const& rhs)
83{
84 return lhs.size() == rhs.size() &&
85 std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
86}
87
88inline bool
89operator!=(SecretKey const& lhs, SecretKey const& rhs)
90{
91 return !(lhs == rhs);
92}
93
94//------------------------------------------------------------------------------
95
97template <>
99parseBase58(TokenType type, std::string const& s);
100
101inline std::string
103{
104 return encodeBase58Token(type, sk.data(), sk.size());
105}
106
108SecretKey
110
112SecretKey
113generateSecretKey(KeyType type, Seed const& seed);
114
116PublicKey
117derivePublicKey(KeyType type, SecretKey const& sk);
118
128generateKeyPair(KeyType type, Seed const& seed);
129
133
140Buffer
141signDigest(PublicKey const& pk, SecretKey const& sk, uint256 const& digest);
142
143inline Buffer
144signDigest(KeyType type, SecretKey const& sk, uint256 const& digest)
145{
146 return signDigest(derivePublicKey(type, sk), sk, digest);
147}
155Buffer
156sign(PublicKey const& pk, SecretKey const& sk, Slice const& message);
157
158inline Buffer
159sign(KeyType type, SecretKey const& sk, Slice const& message)
160{
161 return sign(derivePublicKey(type, sk), sk, message);
162}
165} // namespace ripple
166
167#endif
A secret key.
Definition SecretKey.h:19
std::uint8_t const * data() const
Definition SecretKey.h:37
const_iterator begin() const noexcept
Definition SecretKey.h:57
SecretKey(SecretKey const &)=default
std::uint8_t const * const_iterator
Definition SecretKey.h:24
const_iterator cend() const noexcept
Definition SecretKey.h:75
SecretKey & operator=(SecretKey const &)=default
const_iterator cbegin() const noexcept
Definition SecretKey.h:63
std::size_t size() const
Definition SecretKey.h:43
std::uint8_t buf_[32]
Definition SecretKey.h:21
std::string to_string() const
Convert the secret key to a hexadecimal string.
Definition SecretKey.cpp:50
const_iterator end() const noexcept
Definition SecretKey.h:69
An immutable linear range of bytes.
Definition Slice.h:27
T memcmp(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
TokenType
Definition tokens.h:19
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:213
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
base_uint< 256 > uint256
Definition base_uint.h:539
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition tokens.cpp:181
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:138
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
KeyType
Definition KeyType.h:9
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:566