| 85 | } |
| 86 | |
| 87 | std::string EncodeBase58(Span<const unsigned char> input) |
| 88 | { |
| 89 | // Skip & count leading zeroes. |
| 90 | int zeroes = 0; |
| 91 | int length = 0; |
| 92 | while (input.size() > 0 && input[0] == 0) { |
| 93 | input = input.subspan(1); |
| 94 | zeroes++; |
| 95 | } |
| 96 | // Allocate enough space in big-endian base58 representation. |
| 97 | int size = input.size() * 138 / 100 + 1; // log(256) / log(58), rounded up. |
| 98 | std::vector<unsigned char> b58(size); |
| 99 | // Process the bytes. |
| 100 | while (input.size() > 0) { |
| 101 | int carry = input[0]; |
| 102 | int i = 0; |
| 103 | // Apply "b58 = b58 * 256 + ch". |
| 104 | for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) { |
| 105 | carry += 256 * (*it); |
| 106 | *it = carry % 58; |
| 107 | carry /= 58; |
| 108 | } |
| 109 | |
| 110 | assert(carry == 0); |
| 111 | length = i; |
| 112 | input = input.subspan(1); |
| 113 | } |
| 114 | // Skip leading zeroes in base58 result. |
| 115 | std::vector<unsigned char>::iterator it = b58.begin() + (size - length); |
| 116 | while (it != b58.end() && *it == 0) |
| 117 | it++; |
| 118 | // Translate the result into a string. |
| 119 | std::string str; |
| 120 | str.reserve(zeroes + (b58.end() - it)); |
| 121 | str.assign(zeroes, '1'); |
| 122 | while (it != b58.end()) |
| 123 | str += pszBase58[*(it++)]; |
| 124 | return str; |
| 125 | } |
| 126 | |
| 127 | bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len) |
| 128 | { |