PIVX Core  5.6.99
P2P Digital Currency
base58.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 The Bitcoin developers
2 // Copyright (c) 2017-2021 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "base58.h"
7 
8 #include "hash.h"
9 #include "util/string.h"
10 
11 #include "uint256.h"
12 
13 #include <algorithm>
14 #include <assert.h>
15 #include <sstream>
16 #include <vector>
17 
18 #include <limits>
19 
21 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
22 
23 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch, int max_ret_len)
24 {
25  // Skip leading spaces.
26  while (*psz && isspace(*psz))
27  psz++;
28  // Skip and count leading '1's.
29  int zeroes = 0;
30  int length = 0;
31  while (*psz == '1') {
32  zeroes++;
33  if (zeroes > max_ret_len) return false;
34  psz++;
35  }
36  // Allocate enough space in big-endian base256 representation.
37  int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up.
38  std::vector<unsigned char> b256(size);
39  // Process the characters.
40  while (*psz && !isspace(*psz)) {
41  // Decode base58 character
42  const char* ch = strchr(pszBase58, *psz);
43  if (ch == nullptr)
44  return false;
45  // Apply "b256 = b256 * 58 + ch".
46  int carry = ch - pszBase58;
47  int i = 0;
48  for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
49  carry += 58 * (*it);
50  *it = carry % 256;
51  carry /= 256;
52  }
53  assert(carry == 0);
54  length = i;
55  if (length + zeroes > max_ret_len) return false;
56  psz++;
57  }
58  // Skip trailing spaces.
59  while (isspace(*psz))
60  psz++;
61  if (*psz != 0)
62  return false;
63  // Skip leading zeroes in b256.
64  std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
65  // Copy result into output vector.
66  vch.reserve(zeroes + (b256.end() - it));
67  vch.assign(zeroes, 0x00);
68  while (it != b256.end())
69  vch.push_back(*(it++));
70  return true;
71 }
72 
73 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
74 {
75  // Skip & count leading zeroes.
76  int zeroes = 0;
77  int length = 0;
78  while (pbegin != pend && *pbegin == 0) {
79  pbegin++;
80  zeroes++;
81  }
82  // Allocate enough space in big-endian base58 representation.
83  int size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up.
84  std::vector<unsigned char> b58(size);
85  // Process the bytes.
86  while (pbegin != pend) {
87  int carry = *pbegin;
88  int i = 0;
89  // Apply "b58 = b58 * 256 + ch".
90  for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
91  carry += 256 * (*it);
92  *it = carry % 58;
93  carry /= 58;
94  }
95 
96  assert(carry == 0);
97  length = i;
98  pbegin++;
99  }
100  // Skip leading zeroes in base58 result.
101  std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
102  while (it != b58.end() && *it == 0)
103  it++;
104  // Translate the result into a string.
105  std::string str;
106  str.reserve(zeroes + (b58.end() - it));
107  str.assign(zeroes, '1');
108  while (it != b58.end())
109  str += pszBase58[*(it++)];
110  return str;
111 }
112 
113 std::string EncodeBase58(const std::vector<unsigned char>& vch)
114 {
115  return EncodeBase58(vch.data(), vch.data() + vch.size());
116 }
117 
118 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len)
119 {
120  if (!ValidAsCString(str)) {
121  return false;
122  }
123  return DecodeBase58(str.c_str(), vchRet, max_ret_len);
124 }
125 
126 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
127 {
128  // add 4-byte hash check to the end
129  std::vector<unsigned char> vch(vchIn);
130  uint256 hash = Hash(vch.begin(), vch.end());
131  vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
132  return EncodeBase58(vch);
133 }
134 
135 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len)
136 {
137  if (!DecodeBase58(psz, vchRet, max_ret_len > std::numeric_limits<int>::max() - 4 ? std::numeric_limits<int>::max() : max_ret_len + 4) ||
138  (vchRet.size() < 4)) {
139  vchRet.clear();
140  return false;
141  }
142  // re-calculate the checksum, insure it matches the included 4-byte checksum
143  uint256 hash = Hash(vchRet.begin(), vchRet.end() - 4);
144  if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
145  vchRet.clear();
146  return false;
147  }
148  vchRet.resize(vchRet.size() - 4);
149  return true;
150 }
151 
152 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret)
153 {
154  if (!ValidAsCString(str)) {
155  return false;
156  }
157  return DecodeBase58Check(str.c_str(), vchRet, max_ret);
158 }
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:73
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
Decode a base58-encoded string (psz) that includes a checksum into a byte vector (vchRet),...
Definition: base58.cpp:135
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
Definition: base58.cpp:126
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
Definition: base58.cpp:23
256-bit opaque blob.
Definition: uint256.h:138
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:173
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:44