Auto merge of #2674 - str4d:2469-ci-workers-macos, r=str4d
[VerusCoin.git] / src / base58.cpp
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.
4
5 #include "base58.h"
6
7 #include <hash.h>
8 #include <uint256.h>
9
10 #include <assert.h>
11 #include <string.h>
12
13 /** All alphanumeric characters except for "0", "I", "O", and "l" */
14 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
15
16 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
17 {
18     // Skip leading spaces.
19     while (*psz && isspace(*psz))
20         psz++;
21     // Skip and count leading '1's.
22     int zeroes = 0;
23     while (*psz == '1') {
24         zeroes++;
25         psz++;
26     }
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);
33         if (ch == NULL)
34             return false;
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++) {
38             carry += 58 * (*it);
39             *it = carry % 256;
40             carry /= 256;
41         }
42         assert(carry == 0);
43         psz++;
44     }
45     // Skip trailing spaces.
46     while (isspace(*psz))
47         psz++;
48     if (*psz != 0)
49         return false;
50     // Skip leading zeroes in b256.
51     std::vector<unsigned char>::iterator it = b256.begin();
52     while (it != b256.end() && *it == 0)
53         it++;
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++));
59     return true;
60 }
61
62 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
63 {
64     // Skip & count leading zeroes.
65     int zeroes = 0;
66     while (pbegin != pend && *pbegin == 0) {
67         pbegin++;
68         zeroes++;
69     }
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.
72     // Process the bytes.
73     while (pbegin != pend) {
74         int carry = *pbegin;
75         // Apply "b58 = b58 * 256 + ch".
76         for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); it != b58.rend(); it++) {
77             carry += 256 * (*it);
78             *it = carry % 58;
79             carry /= 58;
80         }
81         assert(carry == 0);
82         pbegin++;
83     }
84     // Skip leading zeroes in base58 result.
85     std::vector<unsigned char>::iterator it = b58.begin();
86     while (it != b58.end() && *it == 0)
87         it++;
88     // Translate the result into a string.
89     std::string str;
90     str.reserve(zeroes + (b58.end() - it));
91     str.assign(zeroes, '1');
92     while (it != b58.end())
93         str += pszBase58[*(it++)];
94     return str;
95 }
96
97 std::string EncodeBase58(const std::vector<unsigned char>& vch)
98 {
99     return EncodeBase58(vch.data(), vch.data() + vch.size());
100 }
101
102 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
103 {
104     return DecodeBase58(str.c_str(), vchRet);
105 }
106
107 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
108 {
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);
114 }
115
116 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
117 {
118     if (!DecodeBase58(psz, vchRet) ||
119         (vchRet.size() < 4)) {
120         vchRet.clear();
121         return false;
122     }
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) {
126         vchRet.clear();
127         return false;
128     }
129     vchRet.resize(vchRet.size() - 4);
130     return true;
131 }
132
133 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
134 {
135     return DecodeBase58Check(str.c_str(), vchRet);
136 }
This page took 0.043998 seconds and 4 git commands to generate.