21 static const char* pszBase58 =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
23 bool DecodeBase58(
const char* psz, std::vector<unsigned char>& vch,
int max_ret_len)
26 while (*psz && isspace(*psz))
33 if (zeroes > max_ret_len)
return false;
37 int size = strlen(psz) * 733 /1000 + 1;
38 std::vector<unsigned char> b256(size);
40 while (*psz && !isspace(*psz)) {
42 const char* ch = strchr(pszBase58, *psz);
46 int carry = ch - pszBase58;
48 for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
55 if (length + zeroes > max_ret_len)
return false;
64 std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
66 vch.reserve(zeroes + (b256.end() - it));
67 vch.assign(zeroes, 0x00);
68 while (it != b256.end())
69 vch.push_back(*(it++));
73 std::string
EncodeBase58(
const unsigned char* pbegin,
const unsigned char* pend)
78 while (pbegin != pend && *pbegin == 0) {
83 int size = (pend - pbegin) * 138 / 100 + 1;
84 std::vector<unsigned char> b58(size);
86 while (pbegin != pend) {
90 for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
101 std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
102 while (it != b58.end() && *it == 0)
106 str.reserve(zeroes + (b58.end() - it));
107 str.assign(zeroes,
'1');
108 while (it != b58.end())
109 str += pszBase58[*(it++)];
115 return EncodeBase58(vch.data(), vch.data() + vch.size());
118 bool DecodeBase58(
const std::string& str, std::vector<unsigned char>& vchRet,
int max_ret_len)
129 std::vector<unsigned char> vch(vchIn);
131 vch.insert(vch.end(), (
unsigned char*)&hash, (
unsigned char*)&hash + 4);
137 if (!
DecodeBase58(psz, vchRet, max_ret_len > std::numeric_limits<int>::max() - 4 ? std::numeric_limits<int>::max() : max_ret_len + 4) ||
138 (vchRet.size() < 4)) {
143 uint256 hash =
Hash(vchRet.begin(), vchRet.end() - 4);
144 if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
148 vchRet.resize(vchRet.size() - 4);
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Why base-58 instead of standard base-64 encoding?
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
Decode a base58-encoded string (psz) that includes a checksum into a byte vector (vchRet),...
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.