MCPcopy Create free account
hub / github.com/JeanLucPons/VanitySearch / bech32_encode

Function bech32_encode

Bech32.cpp:51–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

49};
50
51int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len) {
52 uint32_t chk = 1;
53 size_t i = 0;
54 while (hrp[i] != 0) {
55 int ch = hrp[i];
56 if (ch < 33 || ch > 126) {
57 return 0;
58 }
59
60 if (ch >= 'A' && ch <= 'Z') return 0;
61 chk = bech32_polymod_step(chk) ^ (ch >> 5);
62 ++i;
63 }
64 if (i + 7 + data_len > 90) return 0;
65 chk = bech32_polymod_step(chk);
66 while (*hrp != 0) {
67 chk = bech32_polymod_step(chk) ^ (*hrp & 0x1f);
68 *(output++) = *(hrp++);
69 }
70 *(output++) = '1';
71 for (i = 0; i < data_len; ++i) {
72 if (*data >> 5) return 0;
73 chk = bech32_polymod_step(chk) ^ (*data);
74 *(output++) = charset[*(data++)];
75 }
76 for (i = 0; i < 6; ++i) {
77 chk = bech32_polymod_step(chk);
78 }
79 chk ^= 1;
80 for (i = 0; i < 6; ++i) {
81 *(output++) = charset[(chk >> ((5 - i) * 5)) & 0x1f];
82 }
83 *output = 0;
84 return 1;
85}
86
87int bech32_decode_nocheck(uint8_t *data, size_t *data_len, const char *input) {
88

Callers 1

segwit_addr_encodeFunction · 0.85

Calls 1

bech32_polymod_stepFunction · 0.85

Tested by

no test coverage detected