def block3_to_payload(block3): passphrase_bytes_list = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] passphrase_bytes_list[0] = block3[7:10] passphrase_bytes_list[1][0:2] = block3[10:12] passphrase_bytes_list[1][2] = block3[20] passphrase_bytes_list[2][0:2] = block3[21:23] passphrase_bytes_list[2][2] = block3[24] passphrase_bytes_list[3] = block3[25:28] uid_bytes = bytes(block3[28:32]) passphrase_bytes_list = [bytes(chunk) for chunk in passphrase_bytes_list] passphrase = [base58encode(int.from_bytes(chunk, "little")) for chunk in passphrase_bytes_list] uid = base58encode(int.from_bytes(uid_bytes, "little")) passphrase = '-'.join(passphrase) return passphrase, uid
def handle_block3_fuses(set_block_3, uid, passphrase): if not set_block_3: print("Block 3 eFuses already set. UID: {}, Passphrase valid".format( uid)) return uid, passphrase print("Reading staging password") try: file_directory = os.path.dirname(os.path.realpath(__file__)) with open('staging_password.txt', 'rb') as f: staging_password = f.read().decode('utf-8').split('\n')[0].strip() except: print('staging_password.txt missing or malformed') sys.exit(0) print("Installing auth_handler") if sys.version_info < (3, 5, 3): context = ssl.SSLContext(protocol=ssl.PROTOCOL_SSLv23) else: context = ssl.SSLContext() #context.verify_mode = ssl.CERT_REQUIRED #context.load_verify_locations(certifi.where()) https_handler = urllib.request.HTTPSHandler(context=context) auth_handler = urllib.request.HTTPBasicAuthHandler() auth_handler.add_password(realm='Staging', uri='https://stagingwww.tinkerforge.com', user='******', passwd=staging_password) opener = urllib.request.build_opener(https_handler, auth_handler) urllib.request.install_opener(opener) print("Generating passphrase") # smallest 4-char-base58 string is "2111" = 195112 ("ZZZ"(= 195111) + 1) # largest 4-char-base58 string is "ZZZZ" = 11316495 # Directly selecting chars out of the BASE58 alphabet can result in numbers with leading 1s # (those map to 0, so de- and reencoding will produce the same number without the leading 1) wifi_passphrase = [ base58encode(rnd.randint(base58decode("2111"), base58decode("ZZZZ"))) for i in range(4) ] print("Generating UID") uid = base58encode(get_new_uid()) print("UID: " + uid) print("Generating efuse binary") uid_bytes = base58decode(uid).to_bytes(4, byteorder='little') passphrase_bytes_list = [ base58decode(chunk).to_bytes(3, byteorder='little') for chunk in wifi_passphrase ] #56-95: 5 byte #160-183: 3 byte #192-255: 8 byte # = 16 byte # 4 byte (uid) + 3 byte * 4 (wifi_passphrase) = 16 byte binary = bytearray(32) binary[7:10] = passphrase_bytes_list[0] binary[10:12] = passphrase_bytes_list[1][0:2] binary[20] = passphrase_bytes_list[1][2] binary[21:23] = passphrase_bytes_list[2][0:2] binary[24] = passphrase_bytes_list[2][2] binary[25:28] = passphrase_bytes_list[3] binary[28:32] = uid_bytes with temp_file() as (fd, name): with os.fdopen(fd, 'wb') as f: f.write(binary) print("Burning UID and Wifi passphrase eFuses") espefuse([ "--port", PORT, "burn_block_data", "BLOCK3", name, "--do-not-confirm" ]) return uid, '-'.join(wifi_passphrase)