MCPcopy Create free account
hub / github.com/SecurityInnovation/PGPy / hashdata

Method hashdata

pgpy/pgp.py:397–562  ·  view source on GitHub ↗
(self, subject)

Source from the content-addressed store, hash-verified

395 return h.digest() in self.attested_certifications
396
397 def hashdata(self, subject):
398 _data = bytearray()
399
400 if isinstance(subject, str):
401 try:
402 subject = subject.encode('utf-8')
403 except UnicodeEncodeError:
404 subject = subject.encode('charmap')
405
406 """
407 All signatures are formed by producing a hash over the signature
408 data, and then using the resulting hash in the signature algorithm.
409 """
410
411 if self.type == SignatureType.BinaryDocument:
412 """
413 For binary document signatures (type 0x00), the document data is
414 hashed directly.
415 """
416 if isinstance(subject, (SKEData, IntegrityProtectedSKEData)):
417 _data += subject.__bytearray__()
418 else:
419 _data += bytearray(subject)
420
421 if self.type == SignatureType.CanonicalDocument:
422 """
423 For text document signatures (type 0x01), the
424 document is canonicalized by converting line endings to <CR><LF>,
425 and the resulting data is hashed.
426 """
427 _data += re.subn(br'\r?\n', b'\r\n', subject)[0]
428
429 if self.type in {SignatureType.Generic_Cert, SignatureType.Persona_Cert, SignatureType.Casual_Cert,
430 SignatureType.Positive_Cert, SignatureType.CertRevocation, SignatureType.Subkey_Binding,
431 SignatureType.PrimaryKey_Binding}:
432 """
433 When a signature is made over a key, the hash data starts with the
434 octet 0x99, followed by a two-octet length of the key, and then body
435 of the key packet. (Note that this is an old-style packet header for
436 a key packet with two-octet length.) ...
437 Key revocation signatures (types 0x20 and 0x28)
438 hash only the key being revoked.
439 """
440 _s = b''
441 if isinstance(subject, PGPUID):
442 _s = subject._parent.hashdata
443
444 elif isinstance(subject, PGPKey) and not subject.is_primary:
445 _s = subject._parent.hashdata
446
447 elif isinstance(subject, PGPKey) and subject.is_primary:
448 _s = subject.hashdata
449
450 if len(_s) > 0:
451 _data += b'\x99' + self.int_to_bytes(len(_s), 2) + _s
452
453 if self.type in {SignatureType.Subkey_Binding, SignatureType.PrimaryKey_Binding}:
454 """

Callers

nothing calls this directly

Calls 4

int_to_bytesMethod · 0.80
__hashbytearray__Method · 0.80
__bytearray__Method · 0.45
update_hlenMethod · 0.45

Tested by

no test coverage detected