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& operator= (SecretKey const&) = default;
47 
48  ~SecretKey();
49 
51  SecretKey (Slice const& slice);
52 
53  std::uint8_t const*
54  data() const
55  {
56  return buf_;
57  }
58 
60  size() const
61  {
62  return sizeof(buf_);
63  }
64 
71  to_string() const;
72 
74  begin() const noexcept
75  {
76  return buf_;
77  }
78 
80  cbegin() const noexcept
81  {
82  return buf_;
83  }
84 
86  end() const noexcept
87  {
88  return buf_ + sizeof(buf_);
89  }
90 
92  cend() const noexcept
93  {
94  return buf_ + sizeof(buf_);
95  }
96 };
97 
98 inline
99 bool
101  SecretKey const& rhs)
102 {
103  return lhs.size() == rhs.size() &&
104  std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
105 }
106 
107 inline
108 bool
110  SecretKey const& rhs)
111 {
112  return ! (lhs == rhs);
113 }
114 
115 //------------------------------------------------------------------------------
116 
118 template <>
119 boost::optional<SecretKey>
120 parseBase58 (TokenType type, std::string const& s);
121 
122 inline
124 toBase58 (TokenType type, SecretKey const& sk)
125 {
126  return base58EncodeToken(
127  type, sk.data(), sk.size());
128 }
129 
131 SecretKey
133 
135 SecretKey
136 generateSecretKey (KeyType type, Seed const& seed);
137 
139 PublicKey
140 derivePublicKey (KeyType type, SecretKey const& sk);
141 
151 generateKeyPair (KeyType type, Seed const& seed);
152 
155 randomKeyPair (KeyType type);
156 
163 Buffer
164 signDigest (PublicKey const& pk, SecretKey const& sk,
165  uint256 const& digest);
166 
167 inline
168 Buffer
169 signDigest (KeyType type, SecretKey const& sk,
170  uint256 const& digest)
171 {
172  return signDigest (derivePublicKey(type, sk), sk, digest);
173 }
181 Buffer
182 sign (PublicKey const& pk,
183  SecretKey const& sk, Slice const& message);
184 
185 inline
186 Buffer
187 sign (KeyType type, SecretKey const& sk,
188  Slice const& message)
189 {
190  return sign (derivePublicKey(type, sk), sk, message);
191 }
194 } // ripple
195 
196 #endif
ripple::Dir::const_iterator
Definition: Directory.h:49
std::string
STL class.
ripple::base58EncodeToken
std::string base58EncodeToken(TokenType type, void const *token, std::size_t size)
Definition: tokens.cpp:182
cstring
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:43
std::pair
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:29
ripple::SecretKey::end
const_iterator end() const noexcept
Definition: SecretKey.h:86
ripple::generateKeyPair
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Definition: SecretKey.cpp:267
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:148
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:436
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:44
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:100
ripple::derivePublicKey
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Definition: SecretKey.cpp:228
ripple::operator!=
bool operator!=(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:161
ripple::SecretKey::data
std::uint8_t const * data() const
Definition: SecretKey.h:54
array
ripple::generateSecretKey
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:199
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:92
ripple::randomKeyPair
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition: SecretKey.cpp:286
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::SecretKey::~SecretKey
~SecretKey()
Definition: SecretKey.cpp:34
ripple::sign
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &m)
Generate a signature for a message.
Definition: SecretKey.cpp:132
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:74
ripple::SecretKey::operator=
SecretKey & operator=(SecretKey const &)=default
std::size_t
std::memcmp
T memcmp(T... args)
ripple::SecretKey::cbegin
const_iterator cbegin() const noexcept
Definition: SecretKey.h:80
ripple::SecretKey::size
std::size_t size() const
Definition: SecretKey.h:60
ripple::parseBase58
boost::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:36
ripple::randomSecretKey
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
Definition: SecretKey.cpp:184
ripple::SecretKey::to_string
std::string to_string() const
Convert the secret key to a hexadecimal string.
Definition: SecretKey.cpp:52
string