Skip to content

Commit

Permalink
Merge pull request #311 from prebuilder/fingerprint_class
Browse files Browse the repository at this point in the history
Fingerprint class
  • Loading branch information
Commod0re committed Nov 3, 2022
2 parents d958cfe + 29236fa commit ae2159e
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions pgpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,27 +674,18 @@ class Fingerprint(str):
"""
@property
def keyid(self):
return str(self).replace(' ', '')[-16:]
return self[-16:]

@property
def shortid(self):
return str(self).replace(' ', '')[-8:]
return self[-8:]

def __new__(cls, content):
if isinstance(content, Fingerprint):
return content

# validate input before continuing: this should be a string of 40 hex digits
content = content.upper().replace(' ', '')
if not bool(re.match(r'^[A-F0-9]{40}$', content)):
raise ValueError("Expected: String of 40 hex digits")

# store in the format: "AAAA BBBB CCCC DDDD EEEE FFFF 0000 1111 2222 3333"
# ^^ note 2 spaces here
spaces = [ ' ' if i != 4 else ' ' for i in range(10) ]
chunks = [ ''.join(g) for g in six.moves.zip_longest(*[iter(content)] * 4) ]
content = ''.join(j for i in six.moves.zip_longest(chunks, spaces, fillvalue='') for j in i).strip()

return str.__new__(cls, content)

def __eq__(self, other):
Expand All @@ -713,13 +704,29 @@ def __eq__(self, other):
return False # pragma: no cover

def __ne__(self, other):
return not (self == other)
return str(self) != str(other)

def __hash__(self):
return hash(str(self.replace(' ', '')))
return hash(str(self))

def __bytes__(self):
return binascii.unhexlify(six.b(self.replace(' ', '')))
return binascii.unhexlify(six.b(self))

def __pretty__(self):
content = self
if not bool(re.match(r'^[A-F0-9]{40}$', content)):
raise ValueError("Expected: String of 40 hex digits")

# store in the format: "AAAA BBBB CCCC DDDD EEEE FFFF 0000 1111 2222 3333"
# ^^ note 2 spaces here
spaces = [ ' ' if i != 4 else ' ' for i in range(10) ]
chunks = [ ''.join(g) for g in six.moves.zip_longest(*[iter(content)] * 4) ]
content = ''.join(j for i in six.moves.zip_longest(chunks, spaces, fillvalue='') for j in i).strip()
return content

def __repr__(self):
return self.__class__.__name__+"("+repr(self.__pretty__())+")"



class SorteDeque(collections.deque):
Expand Down

0 comments on commit ae2159e

Please sign in to comment.