Changeset View
Changeset View
Standalone View
Standalone View
src/base58.cpp
Show First 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | bool DecodeBase58(const char *psz, std::vector<uint8_t> &vch, int max_ret_len) { | ||||
vch.reserve(zeroes + (b256.end() - it)); | vch.reserve(zeroes + (b256.end() - it)); | ||||
vch.assign(zeroes, 0x00); | vch.assign(zeroes, 0x00); | ||||
while (it != b256.end()) { | while (it != b256.end()) { | ||||
vch.push_back(*(it++)); | vch.push_back(*(it++)); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
std::string EncodeBase58(const uint8_t *pbegin, const uint8_t *pend) { | std::string EncodeBase58(Span<const uint8_t> input) { | ||||
// Skip & count leading zeroes. | // Skip & count leading zeroes. | ||||
int zeroes = 0; | int zeroes = 0; | ||||
int length = 0; | int length = 0; | ||||
while (pbegin != pend && *pbegin == 0) { | while (input.size() > 0 && input[0] == 0) { | ||||
pbegin++; | input = input.subspan(1); | ||||
zeroes++; | zeroes++; | ||||
} | } | ||||
// Allocate enough space in big-endian base58 representation. | // Allocate enough space in big-endian base58 representation. | ||||
// log(256) / log(58), rounded up. | // log(256) / log(58), rounded up. | ||||
int size = (pend - pbegin) * 138 / 100 + 1; | int size = input.size() * 138 / 100 + 1; | ||||
std::vector<uint8_t> b58(size); | std::vector<uint8_t> b58(size); | ||||
// Process the bytes. | // Process the bytes. | ||||
while (pbegin != pend) { | while (input.size() > 0) { | ||||
int carry = *pbegin; | int carry = input[0]; | ||||
int i = 0; | int i = 0; | ||||
// Apply "b58 = b58 * 256 + ch". | // Apply "b58 = b58 * 256 + ch". | ||||
for (std::vector<uint8_t>::reverse_iterator it = b58.rbegin(); | for (std::vector<uint8_t>::reverse_iterator it = b58.rbegin(); | ||||
(carry != 0 || i < length) && (it != b58.rend()); it++, i++) { | (carry != 0 || i < length) && (it != b58.rend()); it++, i++) { | ||||
carry += 256 * (*it); | carry += 256 * (*it); | ||||
*it = carry % 58; | *it = carry % 58; | ||||
carry /= 58; | carry /= 58; | ||||
} | } | ||||
assert(carry == 0); | assert(carry == 0); | ||||
length = i; | length = i; | ||||
pbegin++; | input = input.subspan(1); | ||||
} | } | ||||
// Skip leading zeroes in base58 result. | // Skip leading zeroes in base58 result. | ||||
std::vector<uint8_t>::iterator it = b58.begin() + (size - length); | std::vector<uint8_t>::iterator it = b58.begin() + (size - length); | ||||
while (it != b58.end() && *it == 0) { | while (it != b58.end() && *it == 0) { | ||||
it++; | it++; | ||||
} | } | ||||
// Translate the result into a string. | // Translate the result into a string. | ||||
std::string str; | std::string str; | ||||
str.reserve(zeroes + (b58.end() - it)); | str.reserve(zeroes + (b58.end() - it)); | ||||
str.assign(zeroes, '1'); | str.assign(zeroes, '1'); | ||||
while (it != b58.end()) { | while (it != b58.end()) { | ||||
str += pszBase58[*(it++)]; | str += pszBase58[*(it++)]; | ||||
} | } | ||||
return str; | return str; | ||||
} | } | ||||
std::string EncodeBase58(const std::vector<uint8_t> &vch) { | |||||
return EncodeBase58(vch.data(), vch.data() + vch.size()); | |||||
} | |||||
bool DecodeBase58(const std::string &str, std::vector<uint8_t> &vchRet, | bool DecodeBase58(const std::string &str, std::vector<uint8_t> &vchRet, | ||||
int max_ret_len) { | int max_ret_len) { | ||||
if (!ValidAsCString(str)) { | if (!ValidAsCString(str)) { | ||||
return false; | return false; | ||||
} | } | ||||
return DecodeBase58(str.c_str(), vchRet, max_ret_len); | return DecodeBase58(str.c_str(), vchRet, max_ret_len); | ||||
} | } | ||||
std::string EncodeBase58Check(const std::vector<uint8_t> &vchIn) { | std::string EncodeBase58Check(Span<const uint8_t> input) { | ||||
// add 4-byte hash check to the end | // add 4-byte hash check to the end | ||||
std::vector<uint8_t> vch(vchIn); | std::vector<uint8_t> vch(input.begin(), input.end()); | ||||
uint256 hash = Hash(vch); | uint256 hash = Hash(vch); | ||||
vch.insert(vch.end(), (uint8_t *)&hash, (uint8_t *)&hash + 4); | vch.insert(vch.end(), (uint8_t *)&hash, (uint8_t *)&hash + 4); | ||||
return EncodeBase58(vch); | return EncodeBase58(vch); | ||||
} | } | ||||
bool DecodeBase58Check(const char *psz, std::vector<uint8_t> &vchRet, | bool DecodeBase58Check(const char *psz, std::vector<uint8_t> &vchRet, | ||||
int max_ret_len) { | int max_ret_len) { | ||||
if (!DecodeBase58(psz, vchRet, | if (!DecodeBase58(psz, vchRet, | ||||
Show All 24 Lines |