diff options
author | Rob Austein <sra@hactrn.net> | 2020-05-26 15:18:19 -0400 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2020-05-26 15:18:19 -0400 |
commit | ed4c22473f5fb07006e773137ed047950e25a4d8 (patch) | |
tree | 26f7b93d5447b2e0cf5bd9c92dcda70161a46545 /cryptech | |
parent | 1cd42f6d3332e1edf78b06bd7dcf51f5a1a7bb23 (diff) |
Wow, python-version-independent hexadecimal is painful
Diffstat (limited to 'cryptech')
-rw-r--r-- | cryptech/libhal.py | 12 |
1 files changed, 10 insertions, 2 deletions
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 |