MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / EncodeBase58

Function EncodeBase58

src/base58.cpp:89–127  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 5

EncodeBase58CheckFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85
FUZZ_TARGETFunction · 0.85
ConsumeScalarRPCArgumentFunction · 0.85
Base58EncodeFunction · 0.85

Calls 7

rbeginMethod · 0.80
rendMethod · 0.80
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
reserveMethod · 0.45
assignMethod · 0.45

Tested by 3

BOOST_AUTO_TEST_CASEFunction · 0.68
FUZZ_TARGETFunction · 0.68
ConsumeScalarRPCArgumentFunction · 0.68