| 66 | } |
| 67 | |
| 68 | string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) { |
| 69 | // Skip & count leading zeroes. |
| 70 | int zeroes = 0; |
| 71 | while (pbegin != pend && *pbegin == 0) { |
| 72 | pbegin++; |
| 73 | zeroes++; |
| 74 | } |
| 75 | // Allocate enough space in big-endian base58 representation. |
| 76 | vector<unsigned char> b58((pend - pbegin) * 138 / 100 + 1); // log(256) / log(58), rounded up. |
| 77 | // Process the bytes. |
| 78 | while (pbegin != pend) { |
| 79 | int carry = *pbegin; |
| 80 | // Apply "b58 = b58 * 256 + ch". |
| 81 | for (vector<unsigned char>::reverse_iterator it = b58.rbegin(); it != b58.rend(); it++) { |
| 82 | carry += 256 * (*it); |
| 83 | *it = carry % 58; |
| 84 | carry /= 58; |
| 85 | } |
| 86 | assert(carry == 0); |
| 87 | pbegin++; |
| 88 | } |
| 89 | // Skip leading zeroes in base58 result. |
| 90 | vector<unsigned char>::iterator it = b58.begin(); |
| 91 | while (it != b58.end() && *it == 0) |
| 92 | it++; |
| 93 | // Translate the result into a string. |
| 94 | string str; |
| 95 | str.reserve(zeroes + (b58.end() - it)); |
| 96 | str.assign(zeroes, '1'); |
| 97 | while (it != b58.end()) |
| 98 | str += pszBase58[*(it++)]; |
| 99 | return str; |
| 100 | } |
| 101 | |
| 102 | string EncodeBase58(const vector<unsigned char>& vch) { |
| 103 | return EncodeBase58(&vch[0], &vch[0] + vch.size()); |