rippled
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 <ripple/basics/Buffer.h>
24 #include <ripple/basics/Slice.h>
25 #include <ripple/protocol/KeyType.h>
26 #include <ripple/protocol/PublicKey.h>
27 #include <ripple/protocol/Seed.h>
28 #include <ripple/protocol/tokens.h>
29 #include <array>
30 #include <cstring>
31 #include <string>
32 
33 namespace ripple {
34 
36 class SecretKey
37 {
38 private:
40 
41 public:
42  using const_iterator = std::uint8_t const*;
43 
44  SecretKey() = default;
45  SecretKey(SecretKey const&) = default;
46  SecretKey&
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 
99 inline bool
100 operator==(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 
106 inline bool
107 operator!=(SecretKey const& lhs, SecretKey const& rhs)
108 {
109  return !(lhs == rhs);
110 }
111 
112 //------------------------------------------------------------------------------
113 
115 template <>
117 parseBase58(TokenType type, std::string const& s);
118 
119 inline std::string
120 toBase58(TokenType type, SecretKey const& sk)
121 {
122  return encodeBase58Token(type, sk.data(), sk.size());
123 }
124 
126 SecretKey
128 
130 SecretKey
131 generateSecretKey(KeyType type, Seed const& seed);
132 
134 PublicKey
135 derivePublicKey(KeyType type, SecretKey const& sk);
136 
146 generateKeyPair(KeyType type, Seed const& seed);
147 
150 randomKeyPair(KeyType type);
151 
158 Buffer
159 signDigest(PublicKey const& pk, SecretKey const& sk, uint256 const& digest);
160 
161 inline Buffer
162 signDigest(KeyType type, SecretKey const& sk, uint256 const& digest)
163 {
164  return signDigest(derivePublicKey(type, sk), sk, digest);
165 }
173 Buffer
174 sign(PublicKey const& pk, SecretKey const& sk, Slice const& message);
175 
176 inline Buffer
177 sign(KeyType type, SecretKey const& sk, Slice const& message)
178 {
179  return sign(derivePublicKey(type, sk), sk, message);
180 }
183 } // namespace ripple
184 
185 #endif
ripple::Dir::const_iterator
Definition: Directory.h:49
std::string
STL class.
cstring
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
std::pair
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
ripple::SecretKey::end
const_iterator end() const noexcept
Definition: SecretKey.h:87
ripple::generateKeyPair
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Definition: SecretKey.cpp:351
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:165
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:47
ripple::SecretKey::buf_
std::uint8_t buf_[32]
Definition: SecretKey.h:39
ripple::base_uint< 256 >
ripple::TokenType
TokenType
Definition: tokens.h:29
ripple::signDigest
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
Definition: SecretKey.cpp:212
ripple::derivePublicKey
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Definition: SecretKey.cpp:313
ripple::operator!=
bool operator!=(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:175
ripple::SecretKey::data
std::uint8_t const * data() const
Definition: SecretKey.h:55
array
ripple::generateSecretKey
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:291
std::uint8_t
ripple::SecretKey
A secret key.
Definition: SecretKey.h:36
ripple::KeyType
KeyType
Definition: KeyType.h:28
ripple::SecretKey::cend
const_iterator cend() const noexcept
Definition: SecretKey.h:93
ripple::randomKeyPair
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition: SecretKey.cpp:368
ripple::SecretKey::SecretKey
SecretKey()=default
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::encodeBase58Token
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition: tokens.cpp:199
ripple::SecretKey::~SecretKey
~SecretKey()
Definition: SecretKey.cpp:33
ripple::sign
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &m)
Generate a signature for a message.
Definition: SecretKey.cpp:238
ripple::SecretKey::const_iterator
std::uint8_t const * const_iterator
Definition: SecretKey.h:42
ripple::SecretKey::begin
const_iterator begin() const noexcept
Definition: SecretKey.h:75
ripple::SecretKey::operator=
SecretKey & operator=(SecretKey const &)=default
std::optional
std::size_t
std::memcmp
T memcmp(T... args)
ripple::parseBase58
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:114
ripple::SecretKey::cbegin
const_iterator cbegin() const noexcept
Definition: SecretKey.h:81
ripple::SecretKey::size
std::size_t size() const
Definition: SecretKey.h:61
ripple::randomSecretKey
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
Definition: SecretKey.cpp:281
ripple::SecretKey::to_string
std::string to_string() const
Convert the secret key to a hexadecimal string.
Definition: SecretKey.cpp:51
string