From 8dc8f4c9cf7d1fc10cc110407a73f55909eeeb9d Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Wed, 10 Jun 2020 00:46:48 -0400 Subject: Whack with club until works with Python 2 and Python 3 String types are still a bit weird. Amost everything here is currently required to be bytes rather than str in Python 3, which feels a bit unnatural, but everything on the wire has to be bytes and we don't really have enough information to know when we should be converting back to str. In the long term, once we ditch Python 2, we may be able to do a bit better for things like the string versions of attribute names, but for the moment it's simplest to stick with bytes. --- cryptech/py11/attributes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'cryptech/py11/attributes.py') diff --git a/cryptech/py11/attributes.py b/cryptech/py11/attributes.py index c6a87da..035677c 100644 --- a/cryptech/py11/attributes.py +++ b/cryptech/py11/attributes.py @@ -15,7 +15,7 @@ class Attribute(object): from . import types assert attribute_name.startswith("CKA_") attribute_number = getattr(constants, attribute_name) - type_class = getattr(types, type_name, str) + type_class = getattr(types, type_name, bytes) if type_class is CK_BBOOL: cls = Attribute_CK_BBOOL elif type_class is CK_ULONG: @@ -35,15 +35,15 @@ class Attribute(object): def decode(self, x): return x class Attribute_CK_BBOOL(Attribute): - def encode(self, x): return chr(int(x)) - def decode(self, x): return bool(ord(x)) + def encode(self, x): return pack("B", int(x)) + def decode(self, x): return bool(unpack("B", x)[0]) class Attribute_CK_ULONG(Attribute): def encode(self, x): return pack("L", x) def decode(self, x): return unpack("L", x)[0] class Attribute_biginteger(Attribute): - def encode(self, x): return "\x00" if x == 0 else unhexlify("{0:0{1}x".format(x, ((x.bit_length() + 7) / 8) * 2)) + def encode(self, x): return "\x00" if x == 0 else unhexlify("{0:0{1}x}".format(x, ((x.bit_length() + 7) // 8) * 2)) def decode(self, x): return int(hexlify(x), 16) -- cgit v1.2.3