15 static const char* pszBase58 =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
16 static const int8_t mapBase58[256] = {
17 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
18 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
19 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
20 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
21 -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1,
22 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1,
23 -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46,
24 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1,
25 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
26 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
27 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
28 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
29 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
30 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
31 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
32 -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
35 bool DecodeBase58(
const char* psz, std::vector<unsigned char>& vch)
48 int size = strlen(psz) * 733 /1000 + 1;
49 std::vector<unsigned char> b256(size);
51 static_assert(
sizeof(mapBase58)/
sizeof(mapBase58[0]) == 256,
"mapBase58.size() should be 256");
52 while (*psz && !
IsSpace(*psz)) {
54 int carry = mapBase58[(uint8_t)*psz];
58 for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
73 std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
74 while (it != b256.end() && *it == 0)
77 vch.reserve(zeroes + (b256.end() - it));
78 vch.assign(zeroes, 0x00);
79 while (it != b256.end())
80 vch.push_back(*(it++));
84 std::string
EncodeBase58(
const unsigned char* pbegin,
const unsigned char* pend)
89 while (pbegin != pend && *pbegin == 0) {
94 int size = (pend - pbegin) * 138 / 100 + 1;
95 std::vector<unsigned char> b58(size);
97 while (pbegin != pend) {
101 for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
102 carry += 256 * (*it);
112 std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
113 while (it != b58.end() && *it == 0)
117 str.reserve(zeroes + (b58.end() - it));
118 str.assign(zeroes,
'1');
119 while (it != b58.end())
120 str += pszBase58[*(it++)];
126 return EncodeBase58(vch.data(), vch.data() + vch.size());
129 bool DecodeBase58(
const std::string& str, std::vector<unsigned char>& vchRet)
137 std::vector<unsigned char> vch(vchIn);
139 vch.insert(vch.end(), (
unsigned char*)&hash, (
unsigned char*)&hash + 4);
146 (vchRet.size() < 4)) {
151 uint256 hash =
Hash(vchRet.begin(), vchRet.end() - 4);
152 if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
156 vchRet.resize(vchRet.size() - 4);
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Why base-58 instead of standard base-64 encoding?
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet)
Decode a base58-encoded string (psz) that includes a checksum into a byte vector (vchRet), return true if decoding is successful.
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.