20 #include <ripple/basics/safe_cast.h>
21 #include <ripple/protocol/tokens.h>
22 #include <ripple/protocol/digest.h>
23 #include <boost/container/small_vector.hpp>
34 "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz";
37 "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
41 template <
class Hasher>
43 typename Hasher::result_type
49 typename Hasher::result_type
>(h);
55 typename Hasher::result_type
58 return digest<Hasher>(v.
data(), v.
size());
62 template <
class Hasher,
class... Args>
64 typename Hasher::result_type
67 return digest<Hasher>(
68 digest<Hasher>(args...));
86 digest2<sha256_hasher>(message, size);
107 char const*
const alphabet)
109 auto pbegin =
reinterpret_cast<unsigned char const*
>(message);
110 auto const pend = pbegin + size;
114 while (pbegin != pend && *pbegin == 0)
120 auto const b58begin =
reinterpret_cast<unsigned char*
>(temp);
121 auto const b58end = b58begin + temp_size;
125 while (pbegin != pend)
129 for (
auto iter = b58end; iter != b58begin; --iter)
131 carry += 256 * (iter[-1]);
132 iter[-1] = carry % 58;
140 auto iter = b58begin;
141 while (iter != b58end && *iter == 0)
146 str.
reserve(zeroes + (b58end - iter));
147 str.
assign(zeroes, alphabet[0]);
148 while (iter != b58end)
149 str += alphabet[*(iter++)];
156 void const* token,
std::size_t size,
char const*
const alphabet)
159 auto const expanded = 1 + size + 4;
164 auto const bufsize = expanded * 3;
166 boost::container::small_vector<std::uint8_t, 1024> buf (bufsize);
170 buf[0] = safe_cast<std::underlying_type_t <TokenType>>(type);
173 checksum(buf.data() + 1 + size, buf.data(), 1 + size);
176 buf.data(), expanded,
177 buf.data() + expanded, bufsize - expanded,
203 template <
class InverseArray>
207 InverseArray
const& inv)
209 auto psz = s.
c_str();
210 auto remain = s.
size();
213 while (remain > 0 && inv[*psz] == 0)
226 remain * 733 / 1000 + 1);
229 auto carry = inv[*psz];
233 for (
auto iter = b256.
rbegin();
234 iter != b256.
rend(); ++iter)
246 b256.
begin(), b256.
end(),[](
unsigned char c)
249 result.
reserve (zeroes + (b256.
end() - iter));
250 result.
assign (zeroes, 0x00);
251 while (iter != b256.
end())
261 template <
class InverseArray>
274 if (type != safe_cast<TokenType>(
static_cast<std::uint8_t>(ret[0])))
301 for(
auto const c : digits)
303 unsigned char>(c)] = i++;
309 return map_[
static_cast<
static std::string decodeBase58(std::string const &s, InverseArray const &inv)
static InverseAlphabet bitcoinInverse(bitcoinAlphabet)
std::string base58EncodeToken(TokenType type, void const *token, std::size_t size)
static InverseAlphabet rippleInverse(rippleAlphabet)
InverseAlphabet(std::string const &digits)
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
static char bitcoinAlphabet[]
std::string base58EncodeTokenBitcoin(TokenType type, void const *token, std::size_t size)
static Hasher::result_type digest2(Args const &... args)
std::array< int, 256 > map_
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static char rippleAlphabet[]
int operator[](char c) const
static std::string encodeBase58(void const *message, std::size_t size, void *temp, std::size_t temp_size, char const *const alphabet)
static std::string decodeBase58Token(std::string const &s, TokenType type, InverseArray const &inv)
void checksum(void *out, void const *message, std::size_t size)
std::string decodeBase58TokenBitcoin(std::string const &s, TokenType type)
Decode a Base58 token using Bitcoin alphabet.
static std::string encodeToken(TokenType type, void const *token, std::size_t size, char const *const alphabet)