-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase58.cpp
77 lines (71 loc) · 2.82 KB
/
base58.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// base-x encoding / decoding
// Copyright (c) 2018 base-x contributors
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
#include <string>
const char * const ALPHABET =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const char ALPHABET_MAP[128] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1,
-1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1,
-1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1
};
int base58encode(const std::string input, int len, unsigned char result[]) {
unsigned char const* bytes = (unsigned const char*)(input.c_str());
unsigned char digits[len * 137 / 100];
int digitslen = 1;
for (int i = 0; i < len; i++) {
unsigned int carry = (unsigned int) bytes[i];
for (int j = 0; j < digitslen; j++) {
carry += (unsigned int) (digits[j]) << 8;
digits[j] = (unsigned char) (carry % 58);
carry /= 58;
}
while (carry > 0) {
digits[digitslen++] = (unsigned char) (carry % 58);
carry /= 58;
}
}
int resultlen = 0;
// leading zero bytes
for (; resultlen < len && bytes[resultlen] == 0;)
result[resultlen++] = '1';
// reverse
for (int i = 0; i < digitslen; i++)
result[resultlen + i] = ALPHABET[digits[digitslen - 1 - i]];
result[digitslen + resultlen] = 0;
return digitslen + resultlen;
}
int base58decode(
const std::string input, int len, unsigned char *result) {
unsigned char const* str = (unsigned const char*)(input.c_str());
result[0] = 0;
int resultlen = 1;
for (int i = 0; i < len; i++) {
unsigned int carry = (unsigned int) ALPHABET_MAP[str[i]];
for (int j = 0; j < resultlen; j++) {
carry += (unsigned int) (result[j]) * 58;
result[j] = (unsigned char) (carry & 0xff);
carry >>= 8;
}
while (carry > 0) {
result[resultlen++] = (unsigned int) (carry & 0xff);
carry >>= 8;
}
}
for (int i = 0; i < len && str[i] == '1'; i++)
result[resultlen++] = 0;
for (int i = resultlen - 1, z = (resultlen >> 1) + (resultlen & 1);
i >= z; i--) {
int k = result[i];
result[i] = result[resultlen - i - 1];
result[resultlen - i - 1] = k;
}
return resultlen;
}