def b16decode(todecode, casefold=False): """Decode a Base16 encoded byte string. todecode is the byte string to decode. Optional casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is False. The decoded byte string is returned. binascii.Error is raised if todecode were incorrectly padded or if there are non-alphabet characters present in the string. """ todecode = _bytes_from_decode_data(todecode) if casefold: todecode = todecode.upper() if re.search(b"[^0-9A-F]", todecode): raise binascii.Error("Non-base16 digit found") return binascii.unhexlify(todecode)
def parse_keycmd(keycmd, Keycode, buttons): if len(keycmd) < 2: raise ValueError("key command not long enough: {}".format(keycmd)) name = keycmd[0] value = keycmd[1:] # s = sleep, k = keys, w = write if name == "s": return name, float(value) elif name == "k": try: return name, [getattr(Keycode, part) for part in value.split("|")] except AttributeError as e: raise ValueError("invalid keycode {}".format(e)) elif name == "w": return name, unhexlify(value).decode('utf8') elif name == "l": return name, (buttons, parse_rgb(value)) else: raise ValueError("unknown key command {}".format(keycmd))
def b32decode(todecode, casefold=False, map01=None): """Decode a Base32 encoded byte string. todecode is the byte string to decode. Optional casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is False. RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O (oh), and for optional mapping of the digit 1 (one) to either the letter I (eye) or letter L (el). The optional argument map01 when not None, specifies which letter the digit 1 should be mapped to (when map01 is not None, the digit 0 is always mapped to the letter O). For security purposes the default is None, so that 0 and 1 are not allowed in the input. The decoded byte string is returned. binascii.Error is raised if the input is incorrectly padded or if there are non-alphabet characters present in the input. """ todecode = _bytes_from_decode_data(todecode) _, leftover = divmod(len(todecode), 8) if leftover: raise binascii.Error("Incorrect padding") # Handle section 2.4 zero and one mapping. The flag map01 will be either # False, or the character to map the digit 1 (one) to. It should be # either L (el) or I (eye). if map01 is not None: map01 = _bytes_from_decode_data(map01) assert len(map01) == 1, repr(map01) todecode = todecode.translate(bytes.maketrans(b"01", b"O" + map01)) if casefold: todecode = todecode.upper() # Strip off pad characters from the right. We need to count the pad # characters because this will tell us how many null bytes to remove from # the end of the decoded string. padchars = todecode.find(b"=") if padchars > 0: padchars = len(todecode) - padchars todecode = todecode[:-padchars] else: padchars = 0 # Now decode the full quanta parts = [] acc = 0 shift = 35 for char in todecode: val = BASE32_REV.get(char) if val is None: raise binascii.Error("Non-base32 digit found") acc += BASE32_REV[char] << shift shift -= 5 if shift < 0: parts.append(binascii.unhexlify(bytes("%010x" % acc, "ascii"))) acc = 0 shift = 35 # Process the last, partial quanta last = binascii.unhexlify(bytes("%010x" % acc, "ascii")) if padchars == 0: last = b"" # No characters elif padchars == 1: last = last[:-1] elif padchars == 3: last = last[:-2] elif padchars == 4: last = last[:-3] elif padchars == 6: last = last[:-4] else: raise binascii.Error("Incorrect padding") parts.append(last) return b"".join(parts)
from adafruit_binascii import hexlify, unhexlify, a2b_base64, b2a_base64 print("-- Binary<->Hex Conversions --") # Binary data. data = b"CircuitPython is Awesome!" print("Original Binary Data: ", data) # Get the hexadecimal representation of the binary data hex_data = hexlify(data) print("Hex Data: ", hex_data) # Verify data assert ( hex_data == b"43697263756974507974686f6e20697320417765736f6d6521", ), "hexlified data does not match expected data." # Get the binary data represented by hex_data bin_data = unhexlify(hex_data) print("Binary Data: ", bin_data) # Verify data assert bin_data == data, "unhexlified binary data does not match original binary data." print("-- Base64 ASCII <-> Binary Conversions --") data = b"Blinka" print("Original Binary Data: ", data) # Convert binary data to a line of ASCII characters in base64 coding. b64_ascii_data = b2a_base64(data) print("Base64 ASCII Data: ", b64_ascii_data) assert b64_ascii_data == b"Qmxpbmth\n", "Expected base64 coding does not match." # Convert a block of base64 data back to binary data. bin_data = a2b_base64(b"Qmxpbmth\n") print("Converted b64 ASCII->Binary Data: ", bin_data)
^xxx xxxx Bit 0 (MSB): 0:Single Wire, 1:I2C; Bit 1-7: Set by Microchip Byte 16: C0 192 1100 0000 Default 7 bit I2C Address: 0xC0>>1: 0x60 ATECC608A-MAHDA Byte 16: 6A 106 0110 1010 Default 7 bit I2C Address: 0x6A>>1: 0x35 ATECC608A-TNGTLS Byte 16: 20 32 0010 0000 Default 7 bit I2C Address: 0x20>>1: 0x10 ATECC608A-UNKNOWN """ CFG_TLS = bytes( bytearray( unhexlify( ( "01 23 00 00 00 00 50 00 00 00 00 00 00 c0 71 00" "20 20 20 20 20 20 20 20 20 20 20 20 20 c0 00 55" "00 83 20 87 20 87 20 87 2f 87 2f 8f 8f 9f 8f af" "20 20 20 20 20 20 20 20 20 20 20 20 20 8f 00 00" "00 00 00 00 00 00 00 00 00 00 00 00 20 20 20 20" "20 20 20 20 20 20 20 20 20 af 8f ff ff ff ff 00" "00 00 00 ff ff ff ff 00 20 20 20 20 20 20 20 20" "20 20 20 20 20 00 00 00 ff ff ff ff ff ff ff ff" "ff ff ff ff 20 20 20 20 20 20 20 20 20 20 20 20" "20 ff ff ff ff 00 00 55 55 ff ff 00 00 00 00 00" "00 33 20 20 20 20 20 20 20 20 20 20 20 20 20 00" "33 00 33 00 33 00 33 00 1c 00 1c 00 1c 00 3c 00" "3c 00 3c 00 3c 20 20 20 20 20 20 20 20 20 20 20" "20 20 00 3c 00 3c 00 3c 00 1c 00" ).replace(" ", "") ) ) ) # Convert I2C address to config byte 16 and update CFG_TLS _CFG_BYTES_LIST = list(bytearray(CFG_TLS)) _CFG_BYTE_16 = bytes(bytearray(unhexlify(hex(_I2C_ADDR << 1).replace("0x", "")))) _CFG_BYTES_LIST_MOD = _CFG_BYTES_LIST[0:16] + list(_CFG_BYTE_16) + _CFG_BYTES_LIST[17:]