def gen_invalid_bech32_vector(template): '''Generate possibly invalid bech32 vector''' no_data = randbool(0.1) to_upper = randbool(0.1) hrp = template[0] witver = template[1] witprog = bytearray(os.urandom(template[2])) if no_data: rv = bech32_encode(hrp, []) else: data = [witver] + convertbits(witprog, 8, 5) if template[3] and not no_data: if template[2] % 5 in {2, 4}: data[-1] |= 1 else: data.append(0) rv = bech32_encode(hrp, data) if template[4]: i = len(rv) - random.randrange(1, 7) rv = rv[:i] + random.choice(CHARSET.replace(rv[i], '')) + rv[i + 1:] if template[5]: i = len(hrp) + 1 + random.randrange(0, len(rv) - len(hrp) - 4) rv = rv[:i] + rv[i:i + 4].upper() + rv[i + 4:] if to_upper: rv = rv.swapcase() return rv
def gen_invalid_bech32_vector(template): '''Generate possibly invalid bech32 vector''' no_data = randbool(0.1) to_upper = randbool(0.1) hrp = template[0] witver = template[1] witprog = bytearray(os.urandom(template[2])) if no_data: rv = bech32_encode(hrp, []) else: data = [witver] + convertbits(witprog, 8, 5) if template[3] and not no_data: if template[2] % 5 in {2, 4}: data[-1] |= 1 else: data.append(0) rv = bech32_encode(hrp, data) if template[4]: i = len(rv) - random.randrange(1, 7) rv = rv[:i] + random.choice(CHARSET.replace(rv[i], '')) + rv[i + 1:] if template[5]: i = len(hrp) + 1 + random.randrange(0, len(rv) - len(hrp) - 4) rv = rv[:i] + rv[i:i + 4].upper() + rv[i + 4:] if to_upper: rv = rv.swapcase() return rv
def tagged(char, l): # Tagged fields need to be zero-padded to 5 bits. while l.len % 5 != 0: l.append('0b0') return bitstring.pack("uint:5, uint:5, uint:5", CHARSET.find(char), (l.len / 5) / 32, (l.len / 5) % 32) + l