Skip to content

Commit

Permalink
HashAlgorithm: treat Unknown explicitly, like PubKeyAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
dkg committed Jun 13, 2023
1 parent e015068 commit 6beb817
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
5 changes: 5 additions & 0 deletions pgpy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def decompress(self, data):

class HashAlgorithm(IntEnum):
"""Supported hash algorithms."""
Unknown = -1
Invalid = 0x00
MD5 = 0x01
SHA1 = 0x02
Expand All @@ -295,6 +296,10 @@ class HashAlgorithm(IntEnum):
#SHA3_384 = 14
#SHA3_512 = 15

@classmethod
def _missing_(cls, val:int) -> 'HashAlgorithm':
return cls.Unknown

def __init__(self, *args):
super(self.__class__, self).__init__()
self._tuned_count = 255
Expand Down
49 changes: 30 additions & 19 deletions pgpy/packet/packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,17 @@ def pubalg_int(self, val:int) -> None:
self.signature = sigs.get(self.pubalg, OpaqueSignature)()

@sdproperty
def halg(self):
def halg(self) -> HashAlgorithm:
return self._halg

@halg.register(int)
@halg.register(HashAlgorithm)
def halg_int(self, val):
try:
self._halg = HashAlgorithm(val)

except ValueError: # pragma: no cover
@halg.register
def halg_int(self, val:int) -> None:
if isinstance(val, HashAlgorithm):
self._halg = val
else:
self._halg = HashAlgorithm(val)
if self._halg is HashAlgorithm.Unknown:
self._opaque_halg = val

@property
def signature(self):
Expand Down Expand Up @@ -424,7 +424,10 @@ def __bytearray__(self):
_bytes.append(self._opaque_pubalg)
else:
_bytes.append(self.pubalg)
_bytes += self.int_to_bytes(self.halg)
if self.halg is HashAlgorithm.Unknown:
_bytes.append(self._opaque_halg)
else:
_bytes.append(self.halg)
_bytes += self.subpackets.__bytearray__()
_bytes += self.hash2
_bytes += self.signature.__bytearray__()
Expand Down Expand Up @@ -452,7 +455,10 @@ def canonical_bytes(self):
_body.append(self._opaque_pubalg)
else:
_body.append(self.pubalg)
_body += self.int_to_bytes(self.halg)
if self.halg is HashAlgorithm.Unknown:
_body.append(self._opaque_halg)
else:
_body.append(self.halg)
_body += self.subpackets.__hashbytearray__()
_body += self.int_to_bytes(0, minlen=2) # empty unhashed subpackets
_body += self.hash2
Expand All @@ -471,6 +477,8 @@ def __copy__(self):
if self._pubalg is PubKeyAlgorithm.Unknown:
spkt._opaque_pubalg = self._opaque_pubalg
spkt._halg = self._halg
if self._halg is HashAlgorithm.Unknown:
spkt._opaque_halg = self._opaque_halg

spkt.subpackets = copy.copy(self.subpackets)
spkt.hash2 = copy.copy(self.hash2)
Expand Down Expand Up @@ -699,17 +707,17 @@ def pubalg_int(self, val):
self.signature = DSASignature()

@sdproperty
def halg(self):
def halg(self) -> HashAlgorithm:
return self._halg

@halg.register(int)
@halg.register(HashAlgorithm)
def halg_int(self, val):
try:
self._halg = HashAlgorithm(val)

except ValueError: # pragma: no cover
@halg.register
def halg_int(self, val:int) -> None:
if isinstance(val, HashAlgorithm):
self._halg = val
else:
self._halg = HashAlgorithm(val)
if self._halg is HashAlgorithm.Unknown:
self._opaque_halg:int = val

@sdproperty
def signer(self):
Expand All @@ -736,7 +744,10 @@ def __bytearray__(self):
_bytes = bytearray()
_bytes += super(OnePassSignatureV3, self).__bytearray__()
_bytes += bytearray([self.sigtype])
_bytes += bytearray([self.halg])
if self.halg is HashAlgorithm.Unknown:
_bytes.append(self._opaque_halg)
else:
_bytes.append(self.halg)
_bytes += bytearray([self.pubalg])
_bytes += binascii.unhexlify(self.signer.encode("latin-1"))
_bytes += bytearray([int(self.nested)])
Expand Down

0 comments on commit 6beb817

Please sign in to comment.