From 3906f3ed9567496401ada4ba71a342215b8785f1 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 20 May 2018 12:34:15 +0300 Subject: [PATCH] BitString ''H notation support --- pyderasn.py | 28 +++++++++++++++++----------- tests/test_pyderasn.py | 3 +-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/pyderasn.py b/pyderasn.py index 0986475..b3ee318 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -1831,6 +1831,8 @@ class BitString(Obj): >>> b.bit_len 88 + >>> BitString("'0A3B5F291CD'H") + BIT STRING 44 bits 0a3b5f291cd0 >>> b = BitString("'010110000000'B") BIT STRING 12 bits 5800 >>> b.bit_len @@ -1919,21 +1921,25 @@ class BitString(Obj): if isinstance(value, (string_types, binary_type)): if ( isinstance(value, string_types) and - value.startswith("'") and - value.endswith("'B") + value.startswith("'") ): - value = value[1:-2] - if not set(value) <= set(("0", "1")): - raise ValueError("B's coding contains unacceptable chars") - return self._bits2octets(value) + if value.endswith("'B"): + value = value[1:-2] + if not set(value) <= set(("0", "1")): + raise ValueError("B's coding contains unacceptable chars") + return self._bits2octets(value) + elif value.endswith("'H"): + value = value[1:-2] + return ( + len(value) * 4, + hexdec(value + ("" if len(value) % 2 == 0 else "0")), + ) + else: + raise InvalidValueType((self.__class__, string_types, binary_type)) elif isinstance(value, binary_type): return (len(value) * 8, value) else: - raise InvalidValueType(( - self.__class__, - string_types, - binary_type, - )) + raise InvalidValueType((self.__class__, string_types, binary_type)) if isinstance(value, tuple): if ( len(value) == 2 and diff --git a/tests/test_pyderasn.py b/tests/test_pyderasn.py index 62954d7..a07c495 100644 --- a/tests/test_pyderasn.py +++ b/tests/test_pyderasn.py @@ -1519,8 +1519,7 @@ class TestBitString(CommonMixin, TestCase): self.assertEqual(len(encoded), obj.tlvlen) def test_x690_vector(self): - vector_payload = hexdec("0A3B5F291CD0") - vector = BitString((len(vector_payload) * 8 - 4, vector_payload)) + vector = BitString("'0A3B5F291CD'H") obj, tail = BitString().decode(hexdec("0307040A3B5F291CD0")) self.assertSequenceEqual(tail, b"") self.assertEqual(obj, vector) -- 2.44.0