rippled
Loading...
Searching...
No Matches
SecretKey.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_SECRETKEY_H_INCLUDED
21#define RIPPLE_PROTOCOL_SECRETKEY_H_INCLUDED
22
23#include <xrpl/basics/Buffer.h>
24#include <xrpl/basics/Slice.h>
25#include <xrpl/protocol/KeyType.h>
26#include <xrpl/protocol/PublicKey.h>
27#include <xrpl/protocol/Seed.h>
28#include <xrpl/protocol/tokens.h>
29#include <array>
30#include <cstring>
31#include <string>
32
33namespace ripple {
34
37{
38private:
40
41public:
43
44 SecretKey() = delete;
45 SecretKey(SecretKey const&) = default;
47 operator=(SecretKey const&) = default;
48
49 ~SecretKey();
50
52 SecretKey(Slice const& slice);
53
54 std::uint8_t const*
55 data() const
56 {
57 return buf_;
58 }
59
61 size() const
62 {
63 return sizeof(buf_);
64 }
65
72 to_string() const;
73
75 begin() const noexcept
76 {
77 return buf_;
78 }
79
81 cbegin() const noexcept
82 {
83 return buf_;
84 }
85
87 end() const noexcept
88 {
89 return buf_ + sizeof(buf_);
90 }
91
93 cend() const noexcept
94 {
95 return buf_ + sizeof(buf_);
96 }
97};
98
99inline bool
100operator==(SecretKey const& lhs, SecretKey const& rhs)
101{
102 return lhs.size() == rhs.size() &&
103 std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
104}
105
106inline bool
107operator!=(SecretKey const& lhs, SecretKey const& rhs)
108{
109 return !(lhs == rhs);
110}
111
112//------------------------------------------------------------------------------
113
115template <>
117parseBase58(TokenType type, std::string const& s);
118
119inline std::string
121{
122 return encodeBase58Token(type, sk.data(), sk.size());
123}
124
126SecretKey
128
130SecretKey
131generateSecretKey(KeyType type, Seed const& seed);
132
134PublicKey
135derivePublicKey(KeyType type, SecretKey const& sk);
136
146generateKeyPair(KeyType type, Seed const& seed);
147
151
158Buffer
159signDigest(PublicKey const& pk, SecretKey const& sk, uint256 const& digest);
160
161inline Buffer
162signDigest(KeyType type, SecretKey const& sk, uint256 const& digest)
163{
164 return signDigest(derivePublicKey(type, sk), sk, digest);
165}
173Buffer
174sign(PublicKey const& pk, SecretKey const& sk, Slice const& message);
175
176inline Buffer
177sign(KeyType type, SecretKey const& sk, Slice const& message)
178{
179 return sign(derivePublicKey(type, sk), sk, message);
180}
183} // namespace ripple
184
185#endif
A secret key.
Definition: SecretKey.h:37
std::uint8_t const * data() const
Definition: SecretKey.h:55
const_iterator begin() const noexcept
Definition: SecretKey.h:75
SecretKey(SecretKey const &)=default
std::uint8_t const * const_iterator
Definition: SecretKey.h:42
const_iterator cend() const noexcept
Definition: SecretKey.h:93
SecretKey & operator=(SecretKey const &)=default
const_iterator cbegin() const noexcept
Definition: SecretKey.h:81
std::size_t size() const
Definition: SecretKey.h:61
std::uint8_t buf_[32]
Definition: SecretKey.h:39
std::string to_string() const
Convert the secret key to a hexadecimal string.
Definition: SecretKey.cpp:51
const_iterator end() const noexcept
Definition: SecretKey.h:87
An immutable linear range of bytes.
Definition: Slice.h:45
T memcmp(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:106
TokenType
Definition: tokens.h:38
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition: Buffer.h:232
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:116
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Definition: SecretKey.cpp:351
base_uint< 256 > uint256
Definition: base_uint.h:557
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition: tokens.cpp:195
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Definition: SecretKey.cpp:313
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:291
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
Definition: SecretKey.cpp:238
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:152
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
Definition: SecretKey.cpp:281
KeyType
Definition: KeyType.h:28
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
Definition: SecretKey.cpp:212
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition: SecretKey.cpp:368
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition: base_uint.h:584