From: Sergey Matveev Date: Fri, 13 Mar 2026 09:59:55 +0000 (+0300) Subject: Use native hexenc/hexdec capabilities X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;p=pyderasn.git Use native hexenc/hexdec capabilities --- diff --git a/pyderasn.py b/pyderasn.py index 968914e..1f9f80d 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -1887,7 +1887,7 @@ class Obj(metaclass=AutoAddSlots): def hexencode(self): """Do hexadecimal encoded :py:meth:`pyderasn.Obj.encode` """ - return hexenc(self.encode()) + return self.encode().hex() def decode( self, @@ -2090,12 +2090,12 @@ class Obj(metaclass=AutoAddSlots): def hexdecode(self, data, *args, **kwargs): """Do :py:meth:`pyderasn.Obj.decode` with hexadecimal decoded data """ - return self.decode(hexdec(data), *args, **kwargs) + return self.decode(bytes.fromhex(data), *args, **kwargs) def hexdecod(self, data, *args, **kwargs): """Do :py:meth:`pyderasn.Obj.decod` with hexadecimal decoded data """ - return self.decod(hexdec(data), *args, **kwargs) + return self.decod(bytes.fromhex(data), *args, **kwargs) @property def expled(self): @@ -2396,7 +2396,7 @@ def pp_console_row( )) if with_blob: if pp.blob.__class__ == bytes: - cols.append(hexenc(pp.blob)) + cols.append(pp.blob.hex()) elif pp.blob.__class__ == tuple: cols.append(", ".join(pp.blob)) if pp.optional: @@ -2418,7 +2418,7 @@ def pp_console_blob(pp, decode_path_len_decrease=0): if decode_path_len > 0: cols.append(" ." * (decode_path_len + 1)) if pp.blob.__class__ == bytes: - blob = hexenc(pp.blob).upper() + blob = pp.blob.hex().upper() for i in range(0, len(blob), 32): chunk = blob[i:i + 32] yield " ".join(cols + [colonize_hex(chunk)]) @@ -3178,7 +3178,7 @@ class BitString(Obj): value = value[1:-2] return ( len(value) * 4, - hexdec(value + ("" if len(value) % 2 == 0 else "0")), + bytes.fromhex(value + ("" if len(value) % 2 == 0 else "0")), ) if value.__class__ == bytes: return (len(value) * 8, value) @@ -4657,15 +4657,15 @@ class CommonString(OctetString): Everything resembles :py:class:`pyderasn.OctetString`, except ability to deal with unicode text strings. - >>> hexenc("привет мир".encode("utf-8")) + >>> "привет мир".encode("utf-8").hex() 'd0bfd180d0b8d0b2d0b5d18220d0bcd0b8d180' - >>> UTF8String("привет мир") == UTF8String(hexdec("d0...80")) + >>> UTF8String("привет мир") == UTF8String(bytes.fromhex("d0...80")) True >>> s = UTF8String("привет мир") UTF8String UTF8String привет мир >>> str(s) 'привет мир' - >>> hexenc(bytes(s)) + >>> bytes(s).hex() 'd0bfd180d0b8d0b2d0b5d18220d0bcd0b8d180' >>> PrintableString("привет мир") @@ -4678,7 +4678,7 @@ class CommonString(OctetString): >>> s = BMPString("ад", bounds=(2, 2)) >>> s.encoding 'utf-16-be' - >>> hexenc(bytes(s)) + >>> bytes(s).hex() '04300434' .. list-table:: @@ -5791,9 +5791,9 @@ class PrimitiveTypes(Choice): It could be useful for general decoding of some unspecified values: - >>> PrimitiveTypes().decod(hexdec("0403666f6f")).value + >>> PrimitiveTypes().decod(bytes.fromhex("0403666f6f")).value OCTET STRING 3 bytes 666f6f - >>> PrimitiveTypes().decod(hexdec("0203123456")).value + >>> PrimitiveTypes().decod(bytes.fromhex("0203123456")).value INTEGER 1193046 """ __slots__ = () @@ -5835,8 +5835,8 @@ class Any(Obj): ANY INTEGER -123 (0X:7B) >>> a = Any(OctetString(b"hello world").encode()) ANY 040b68656c6c6f20776f726c64 - >>> hexenc(bytes(a)) - b'0x040x0bhello world' + >>> bytes(a).hex() + '0x040x0bhello world' """ __slots__ = ("defined",) tag_default = tag_encode(0) @@ -7483,7 +7483,7 @@ def hexdump(raw): [...], ] """ - hexed = hexenc(raw).upper() + hexed = raw.hex().upper() addr, cols = 0, ["%08x " % 0] for i in range(0, len(hexed), 2): if i != 0 and i // 2 % 8 == 0: @@ -7657,7 +7657,7 @@ def browse(raw, obj, oid_maps=()): ("header", "Hexadecimal: "), colonize_hex(pp.obj.tohex()), ]) if pp.blob.__class__ == bytes: - blob = hexenc(pp.blob).upper() + blob = pp.blob.hex().upper() for i in range(0, len(blob), 32): lines.append([colonize_hex(blob[i:i + 32])]) elif pp.blob.__class__ == tuple: @@ -7953,7 +7953,7 @@ def main(): # pragma: no cover obj, tail = schema().decode(raw, ctx=ctx) print(pprinter(obj)) if tail != b"": - print("\nTrailing data: %s" % hexenc(tail)) + print("\nTrailing data: %s" % tail.hex()) if __name__ == "__main__":