From ed4c22473f5fb07006e773137ed047950e25a4d8 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Tue, 26 May 2020 15:18:19 -0400 Subject: Wow, python-version-independent hexadecimal is painful --- cryptech_muxd | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'cryptech_muxd') diff --git a/cryptech_muxd b/cryptech_muxd index 6c21f7e..7a85a06 100755 --- a/cryptech_muxd +++ b/cryptech_muxd @@ -45,7 +45,6 @@ import atexit import weakref import logging import argparse -import binascii import logging.handlers import serial @@ -65,6 +64,14 @@ from cryptech.libhal import HAL_OK, RPC_FUNC_GET_VERSION, RPC_FUNC_LOGOUT, RPC_F logger = logging.getLogger("cryptech_muxd") +if sys.version_info.major == 2: + def colon_hex(raw): + return ":".join("{:02x}".format(ord(b)) for b in raw) +else: + def colon_hex(raw): + return ":".join("{:02x}".format(b) for b in raw) + + SLIP_END = b"\300" # Indicates end of SLIP packet SLIP_ESC = b"\333" # Indicates byte stuffing SLIP_ESC_END = b"\334" # ESC ESC_END means END data byte @@ -174,7 +181,7 @@ class RPCIOStream(SerialIOStream): @tornado.gen.coroutine def rpc_input(self, query, handle = 0, queue = None): "Send a query to the HSM." - logger.debug("RPC send: %s", ":".join(binascii.hexlify(c) for c in query)) + logger.debug("RPC send: %s", colon_hex(query)) if queue is not None: self.queues[handle] = queue with (yield self.rpc_input_lock.acquire()): @@ -193,7 +200,7 @@ class RPCIOStream(SerialIOStream): for q in self.queues.values(): q.put_nowait(None) return - logger.debug("RPC recv: %s", ":".join(binascii.hexlify(c) for c in reply)) + logger.debug("RPC recv: %s", colon_hex(reply)) if reply == SLIP_END: continue try: @@ -371,7 +378,7 @@ class ProbeIOStream(SerialIOStream): yield tornado.gen.sleep(0.5) response = yield self.read_bytes(self.read_chunk_size, partial = True) - logger.debug("Probing %s: %r %s", self.serial_device, response, ":".join(binascii.hexlify(c) for c in response)) + logger.debug("Probing %s: %r %s", self.serial_device, response, colon_hex(response)) is_cty = any(prompt in response for prompt in (b"Username:", b"Password:", b"cryptech>")) -- cgit v1.2.3