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/libhal.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'cryptech') diff --git a/cryptech/libhal.py b/cryptech/libhal.py index d7bc328..56712cb 100644 --- a/cryptech/libhal.py +++ b/cryptech/libhal.py @@ -39,6 +39,7 @@ A Python interface to the Cryptech libhal RPC API. # not likely to want to use the full ONC RPC mechanism. import os +import sys import uuid import xdrlib import socket @@ -47,6 +48,13 @@ import binascii logger = logging.getLogger(__name__) +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 packet SLIP_ESC = b"\333" # indicates byte stuffing @@ -466,7 +474,7 @@ class HSM(object): def _send(self, msg): # Expects an xdrlib.Packer msg = slip_encode(msg.get_buffer()) if self.debug_io: - logger.debug("send: %s", ":".join(binascii.hexlify(c) for c in msg)) + logger.debug("send: %s", colon_hex(msg)) self.socket.sendall(msg) def _recv(self, code): # Returns a ContextManagedUnpacker @@ -478,7 +486,7 @@ class HSM(object): raise HAL_ERROR_RPC_TRANSPORT() msg.append(self.sockfile.read(1)) if self.debug_io: - logger.debug("recv: %s", ":".join(binascii.hexlify(c) for c in msg)) + logger.debug("recv: %s", colon_hex(msg)) msg = slip_decode(b"".join(msg)) if not msg: continue -- cgit v1.2.3