X-Git-Url: http://www.git.cypherpunks.ru/?p=pyderasn.git;a=blobdiff_plain;f=pyderasn.py;h=116ef6e9e8d899fb2dfa95e431b6915045993c4c;hp=4a3692795c3974b760b1a20fd9971c05308c078a;hb=b54295efb44566cd7eb6b09a4d5366f82cb96441;hpb=6e7c396eadd0193a18870650b6c23abbacea1591 diff --git a/pyderasn.py b/pyderasn.py index 4a36927..116ef6e 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -4,7 +4,7 @@ # pylint: disable=line-too-long,superfluous-parens,protected-access,too-many-lines # pylint: disable=too-many-return-statements,too-many-branches,too-many-statements # PyDERASN -- Python ASN.1 DER/CER/BER codec with abstract structures -# Copyright (C) 2017-2021 Sergey Matveev +# Copyright (C) 2017-2022 Sergey Matveev # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as @@ -971,12 +971,12 @@ _____________ UTCTime _______ .. autoclass:: pyderasn.UTCTime - :members: __init__, todatetime + :members: __init__, todatetime, totzdatetime GeneralizedTime _______________ .. autoclass:: pyderasn.GeneralizedTime - :members: __init__, todatetime + :members: __init__, todatetime, totzdatetime Special types ------------- @@ -1159,8 +1159,6 @@ Now you can print only the specified tree, for example signature algorithm:: """ from array import array -from codecs import getdecoder -from codecs import getencoder from collections import namedtuple from collections import OrderedDict from copy import copy @@ -1182,7 +1180,13 @@ except ImportError: # pragma: no cover def colored(what, *args, **kwargs): return what -__version__ = "9.0" +try: + from dateutil.tz import UTC as tzUTC +except ImportError: # pragma: no cover + tzUTC = "missing" + + +__version__ = "9.1" __all__ = ( "agg_octet_string", @@ -1441,20 +1445,16 @@ class BoundsError(ASN1Error): # Basic coders ######################################################################## -_hexdecoder = getdecoder("hex") -_hexencoder = getencoder("hex") - - def hexdec(data): """Binary data to hexadecimal string convert """ - return _hexdecoder(data)[0] + return bytes.fromhex(data) def hexenc(data): """Hexadecimal string to binary data convert """ - return _hexencoder(data)[0].decode("ascii") + return data.hex() def int_bytes_len(num, byte_len=8): @@ -5038,6 +5038,8 @@ class UTCTime(VisibleString): datetime.datetime(2017, 9, 30, 22, 7, 50) >>> UTCTime(datetime(2057, 9, 30, 22, 7, 50)).todatetime() datetime.datetime(1957, 9, 30, 22, 7, 50) + >>> UTCTime(datetime(2057, 9, 30, 22, 7, 50)).totzdatetime() + datetime.datetime(1957, 9, 30, 22, 7, 50, tzinfo=tzutc()) If BER encoded value was met, then ``ber_raw`` attribute will hold its raw representation. @@ -5247,6 +5249,12 @@ class UTCTime(VisibleString): def todatetime(self): return self._value + def totzdatetime(self): + try: + return self._value.replace(tzinfo=tzUTC) + except TypeError as err: + raise NotImplementedError("Missing dateutil.tz") from err + def __repr__(self): return pp_console_row(next(self.pps())) @@ -6107,7 +6115,7 @@ SequenceState = namedtuple( ) -class SequenceEncode1stMixing: +class SequenceEncode1stMixin: __slots__ = () def _encode1st(self, state): @@ -6121,7 +6129,7 @@ class SequenceEncode1stMixing: return len(self.tag) + len_size(vlen) + vlen, state -class Sequence(SequenceEncode1stMixing, Obj): +class Sequence(SequenceEncode1stMixin, Obj): """``SEQUENCE`` structure type You have to make specification of sequence:: @@ -6619,7 +6627,7 @@ class Sequence(SequenceEncode1stMixing, Obj): yield pp -class Set(Sequence, SequenceEncode1stMixing): +class Set(Sequence, SequenceEncode1stMixin): """``SET`` structure type Its usage is identical to :py:class:`pyderasn.Sequence`. @@ -6822,7 +6830,7 @@ SequenceOfState = namedtuple( ) -class SequenceOf(SequenceEncode1stMixing, Obj): +class SequenceOf(SequenceEncode1stMixin, Obj): """``SEQUENCE OF`` sequence type For that kind of type you must specify the object it will carry on