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
30#include <array>
31#include <cstring>
32#include <string>
33
34namespace ripple {
35
38{
39private:
41
42public:
44
45 SecretKey() = delete;
46 SecretKey(SecretKey const&) = default;
48 operator=(SecretKey const&) = default;
49
50 ~SecretKey();
51
53 SecretKey(Slice const& slice);
54
55 std::uint8_t const*
56 data() const
57 {
58 return buf_;
59 }
60
62 size() const
63 {
64 return sizeof(buf_);
65 }
66
73 to_string() const;
74
76 begin() const noexcept
77 {
78 return buf_;
79 }
80
82 cbegin() const noexcept
83 {
84 return buf_;
85 }
86
88 end() const noexcept
89 {
90 return buf_ + sizeof(buf_);
91 }
92
94 cend() const noexcept
95 {
96 return buf_ + sizeof(buf_);
97 }
98};
99
100inline bool
101operator==(SecretKey const& lhs, SecretKey const& rhs)
102{
103 return lhs.size() == rhs.size() &&
104 std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
105}
106
107inline bool
108operator!=(SecretKey const& lhs, SecretKey const& rhs)
109{
110 return !(lhs == rhs);
111}
112
113//------------------------------------------------------------------------------
114
116template <>
118parseBase58(TokenType type, std::string const& s);
119
120inline std::string
122{
123 return encodeBase58Token(type, sk.data(), sk.size());
124}
125
127SecretKey
129
131SecretKey
132generateSecretKey(KeyType type, Seed const& seed);
133
135PublicKey
136derivePublicKey(KeyType type, SecretKey const& sk);
137
147generateKeyPair(KeyType type, Seed const& seed);
148
152
159Buffer
160signDigest(PublicKey const& pk, SecretKey const& sk, uint256 const& digest);
161
162inline Buffer
163signDigest(KeyType type, SecretKey const& sk, uint256 const& digest)
164{
165 return signDigest(derivePublicKey(type, sk), sk, digest);
166}
174Buffer
175sign(PublicKey const& pk, SecretKey const& sk, Slice const& message);
176
177inline Buffer
178sign(KeyType type, SecretKey const& sk, Slice const& message)
179{
180 return sign(derivePublicKey(type, sk), sk, message);
181}
184} // namespace ripple
185
186#endif
A secret key.
Definition SecretKey.h:38
std::uint8_t const * data() const
Definition SecretKey.h:56
const_iterator begin() const noexcept
Definition SecretKey.h:76
SecretKey(SecretKey const &)=default
std::uint8_t const * const_iterator
Definition SecretKey.h:43
const_iterator cend() const noexcept
Definition SecretKey.h:94
SecretKey & operator=(SecretKey const &)=default
const_iterator cbegin() const noexcept
Definition SecretKey.h:82
std::size_t size() const
Definition SecretKey.h:62
std::uint8_t buf_[32]
Definition SecretKey.h:40
std::string to_string() const
Convert the secret key to a hexadecimal string.
Definition SecretKey.cpp:69
const_iterator end() const noexcept
Definition SecretKey.h:88
An immutable linear range of bytes.
Definition Slice.h:46
T memcmp(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
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.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
base_uint< 256 > uint256
Definition base_uint.h:558
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition tokens.cpp:199
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:156
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
KeyType
Definition KeyType.h:28
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:585