def test_der_encode_decode_long_seq(self):
        for length in (1, 127, 128, 129, 255, 256, 257):
            raw_int = bytes((i & 0xfe) + 1 for i in range(length))
            for leading_zeros in range(3):
                encoded = der.encode_seq((b"\x00" * leading_zeros + raw_int,))
                decoded = der.decode_seq(encoded)
                self.assertEqual(decoded, [raw_int])

        for zeroes in range(3):
            encoded = der.encode_seq((b"\x00" * zeroes,))
            decoded = der.decode_seq(encoded)
            self.assertEqual(decoded, [b"\x00"])
Exemple #2
0
def decode_der_signature(der_signature: memoryview) -> bytearray:
    seq = der.decode_seq(der_signature)
    if len(seq) != 2 or any(len(i) > 32 for i in seq):
        raise ValueError

    signature = bytearray(64)
    signature[32 - len(seq[0]) : 32] = seq[0]
    signature[64 - len(seq[1]) : 64] = seq[1]

    return signature
    def test_der_encode_seq(self):

        for s, d in self.vectors_seq:
            s = tuple(unhexlify(i) for i in s)
            d = unhexlify(d)
            d2 = der.encode_seq(s)
            self.assertEqual(d2, d)
            s = [i.lstrip(b"\x00") for i in s]
            s2 = der.decode_seq(d)
            self.assertEqual(s2, s)