1 // Copyright (c) 2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
13 /** All alphanumeric characters except for "0", "I", "O", and "l" */
14 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
16 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
18 // Skip leading spaces.
19 while (*psz && isspace(*psz))
21 // Skip and count leading '1's.
27 // Allocate enough space in big-endian base256 representation.
28 std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up.
29 // Process the characters.
30 while (*psz && !isspace(*psz)) {
31 // Decode base58 character
32 const char* ch = strchr(pszBase58, *psz);
35 // Apply "b256 = b256 * 58 + ch".
36 int carry = ch - pszBase58;
37 for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) {
45 // Skip trailing spaces.
50 // Skip leading zeroes in b256.
51 std::vector<unsigned char>::iterator it = b256.begin();
52 while (it != b256.end() && *it == 0)
54 // Copy result into output vector.
55 vch.reserve(zeroes + (b256.end() - it));
56 vch.assign(zeroes, 0x00);
57 while (it != b256.end())
58 vch.push_back(*(it++));
62 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
64 // Skip & count leading zeroes.
66 while (pbegin != pend && *pbegin == 0) {
70 // Allocate enough space in big-endian base58 representation.
71 std::vector<unsigned char> b58((pend - pbegin) * 138 / 100 + 1); // log(256) / log(58), rounded up.
73 while (pbegin != pend) {
75 // Apply "b58 = b58 * 256 + ch".
76 for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); it != b58.rend(); it++) {
84 // Skip leading zeroes in base58 result.
85 std::vector<unsigned char>::iterator it = b58.begin();
86 while (it != b58.end() && *it == 0)
88 // Translate the result into a string.
90 str.reserve(zeroes + (b58.end() - it));
91 str.assign(zeroes, '1');
92 while (it != b58.end())
93 str += pszBase58[*(it++)];
97 std::string EncodeBase58(const std::vector<unsigned char>& vch)
99 return EncodeBase58(vch.data(), vch.data() + vch.size());
102 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
104 return DecodeBase58(str.c_str(), vchRet);
107 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
109 // add 4-byte hash check to the end
110 std::vector<unsigned char> vch(vchIn);
111 uint256 hash = Hash(vch.begin(), vch.end());
112 vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
113 return EncodeBase58(vch);
116 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
118 if (!DecodeBase58(psz, vchRet) ||
119 (vchRet.size() < 4)) {
123 // re-calculate the checksum, insure it matches the included 4-byte checksum
124 uint256 hash = Hash(vchRet.begin(), vchRet.end() - 4);
125 if (memcmp(&hash, &vchRet.end()[-4], 4) != 0) {
129 vchRet.resize(vchRet.size() - 4);
133 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
135 return DecodeBase58Check(str.c_str(), vchRet);