BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
base58.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2018 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.
4 
5 #include <base58.h>
6 
7 #include <hash.h>
8 #include <uint256.h>
9 #include <utilstrencodings.h>
10 
11 #include <assert.h>
12 #include <string.h>
13 
15 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
16 static const int8_t mapBase58[256] = {
17  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
18  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
19  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
20  -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
21  -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1,
22  22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1,
23  -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46,
24  47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1,
25  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
26  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
27  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
28  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
29  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
30  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
31  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
32  -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
33 };
34 
35 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
36 {
37  // Skip leading spaces.
38  while (*psz && IsSpace(*psz))
39  psz++;
40  // Skip and count leading '1's.
41  int zeroes = 0;
42  int length = 0;
43  while (*psz == '1') {
44  zeroes++;
45  psz++;
46  }
47  // Allocate enough space in big-endian base256 representation.
48  int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up.
49  std::vector<unsigned char> b256(size);
50  // Process the characters.
51  static_assert(sizeof(mapBase58)/sizeof(mapBase58[0]) == 256, "mapBase58.size() should be 256"); // guarantee not out of range
52  while (*psz && !IsSpace(*psz)) {
53  // Decode base58 character
54  int carry = mapBase58[(uint8_t)*psz];
55  if (carry == -1) // Invalid b58 character
56  return false;
57  int i = 0;
58  for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
59  carry += 58 * (*it);
60  *it = carry % 256;
61  carry /= 256;
62  }
63  assert(carry == 0);
64  length = i;
65  psz++;
66  }
67  // Skip trailing spaces.
68  while (IsSpace(*psz))
69  psz++;
70  if (*psz != 0)
71  return false;
72  // Skip leading zeroes in b256.
73  std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
74  while (it != b256.end() && *it == 0)
75  it++;
76  // Copy result into output vector.
77  vch.reserve(zeroes + (b256.end() - it));
78  vch.assign(zeroes, 0x00);
79  while (it != b256.end())
80  vch.push_back(*(it++));
81  return true;
82 }
83 
84 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
85 {
86  // Skip & count leading zeroes.
87  int zeroes = 0;
88  int length = 0;
89  while (pbegin != pend && *pbegin == 0) {
90  pbegin++;
91  zeroes++;
92  }
93  // Allocate enough space in big-endian base58 representation.
94  int size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up.
95  std::vector<unsigned char> b58(size);
96  // Process the bytes.
97  while (pbegin != pend) {
98  int carry = *pbegin;
99  int i = 0;
100  // Apply "b58 = b58 * 256 + ch".
101  for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
102  carry += 256 * (*it);
103  *it = carry % 58;
104  carry /= 58;
105  }
106 
107  assert(carry == 0);
108  length = i;
109  pbegin++;
110  }
111  // Skip leading zeroes in base58 result.
112  std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
113  while (it != b58.end() && *it == 0)
114  it++;
115  // Translate the result into a string.
116  std::string str;
117  str.reserve(zeroes + (b58.end() - it));
118  str.assign(zeroes, '1');
119  while (it != b58.end())
120  str += pszBase58[*(it++)];
121  return str;
122 }
123 
124 std::string EncodeBase58(const std::vector<unsigned char>& vch)
125 {
126  return EncodeBase58(vch.data(), vch.data() + vch.size());
127 }
128 
129 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
130 {
131  return DecodeBase58(str.c_str(), vchRet);
132 }
133 
134 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
135 {
136  // add 4-byte hash check to the end
137  std::vector<unsigned char> vch(vchIn);
138  uint256 hash = Hash(vch.begin(), vch.end());
139  vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
140  return EncodeBase58(vch);
141 }
142 
143 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
144 {
145  if (!DecodeBase58(psz, vchRet) ||
146  (vchRet.size() < 4)) {
147  vchRet.clear();
148  return false;
149  }
150  // re-calculate the checksum, ensure it matches the included 4-byte checksum
151  uint256 hash = Hash(vchRet.begin(), vchRet.end() - 4);
152  if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
153  vchRet.clear();
154  return false;
155  }
156  vchRet.resize(vchRet.size() - 4);
157  return true;
158 }
159 
160 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
161 {
162  return DecodeBase58Check(str.c_str(), vchRet);
163 }
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
Definition: base58.cpp:35
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:84
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:119
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet)
Decode a base58-encoded string (psz) that includes a checksum into a byte vector (vchRet), return true if decoding is successful.
Definition: base58.cpp:143
256-bit opaque blob.
Definition: uint256.h:122
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
Definition: base58.cpp:134
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.