27 #include <sys/types.h>
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1,
36 -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1,
37 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1,
38 -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46,
39 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
44 size_t binsz = *binszp;
45 const unsigned char *b58u = (
const void *)b58;
46 unsigned char *binu = bin;
47 size_t outisz = (binsz + 3) / 4;
48 uint32_t outi[outisz];
52 uint8_t bytesleft = binsz % 4;
53 uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0;
54 unsigned zerocount = 0;
59 memset(outi, 0, outisz *
sizeof(*outi));
66 for ( ; i < b58sz; ++i) {
76 for (j = outisz; j--; ) {
77 t = ((uint64_t)outi[j]) * 58 + c;
78 c = (t & 0x3f00000000) >> 32;
79 outi[j] = t & 0xffffffff;
83 memset(outi, 0, outisz *
sizeof(*outi));
86 if (outi[0] & zeromask) {
88 memset(outi, 0, outisz *
sizeof(*outi));
96 *(binu++) = (outi[0] & 0xff0000) >> 16;
98 *(binu++) = (outi[0] & 0xff00) >> 8;
100 *(binu++) = (outi[0] & 0xff);
106 for (; j < outisz; ++j) {
107 *(binu++) = (outi[j] >> 0x18) & 0xff;
108 *(binu++) = (outi[j] >> 0x10) & 0xff;
109 *(binu++) = (outi[j] >> 8) & 0xff;
110 *(binu++) = (outi[j] >> 0) & 0xff;
115 for (i = 0; i < binsz; ++i) {
121 *binszp += zerocount;
123 memset(outi, 0, outisz *
sizeof(*outi));
129 unsigned char buf[32];
130 const uint8_t *binc = bin;
137 if (memcmp(&binc[binsz - 4], buf, 4)) {
142 for (i = 0; binc[i] ==
'\0' && base58str[i] ==
'1'; ++i) {
144 if (binc[i] ==
'\0' || base58str[i] ==
'1') {
152 "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
156 const uint8_t *bin = data;
158 ssize_t i, j, high, zcount = 0;
161 while (zcount < (ssize_t)binsz && !bin[zcount]) {
165 size = (binsz - zcount) * 138 / 100 + 1;
167 memset(buf, 0, size);
169 for (i = zcount, high = size - 1; i < (ssize_t)binsz; ++i, high = j) {
170 for (carry = bin[i], j = size - 1; (j > high) || carry; --j) {
171 carry += 256 * buf[j];
177 for (j = 0; j < (ssize_t)size && !buf[j]; ++j);
179 if (*b58sz <= zcount + size - j) {
180 *b58sz = zcount + size - j + 1;
181 memset(buf, 0, size);
186 memset(b58,
'1', zcount);
188 for (i = zcount; j < (ssize_t)size; ++i, ++j) {
194 memset(buf, 0, size);
204 uint8_t buf[datalen + 32];
205 uint8_t *hash = buf + datalen;
206 memcpy(buf, data, datalen);
209 size_t res = strsize;
215 memset(buf, 0,
sizeof(buf));
222 size_t strl = strlen(str);
226 if (strl > 128 || datalen < strl) {
230 size_t binsize = strl;
235 memmove(data, data+strl-binsize, binsize);
236 memset(data+binsize, 0, datalen-binsize);
int btc_base58_decode(void *bin, size_t *binszp, const char *b58)
void sha256_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
int btc_base58_decode_check(const char *str, uint8_t *data, size_t datalen)
static const int8_t b58digits_map[]
int btc_b58check(const void *bin, size_t binsz, const char *base58str)
static const char b58digits_ordered[]
int btc_base58_encode(char *b58, size_t *b58sz, const void *data, size_t binsz)
int btc_base58_encode_check(const uint8_t *data, int datalen, char *str, int strsize)