def to_signable_data(self, signature_type, signature_version=3): result = bytearray() if self.version >= 4: result.append(self.version) result.append(self.signature_type) if self.version < 4: result.extend( utils.int_to_4byte(time.mktime( self.creation_time.timetuple()))) else: result.append(self.public_key_algorithm) result.append(self.hash_algorithm) hashed_subpacket_data = b''.join(map(bytes, self.hashed_subpackets)) hashed_subpacket_length = len(hashed_subpacket_data) result.extend(utils.int_to_2byte(hashed_subpacket_length)) result.extend(hashed_subpacket_data) result.append(self.version) result.append(255) result.extend(utils.int_to_4byte(hashed_subpacket_length + 6)) return result
def to_signable_data(self, signature_type, signature_version=3): result = bytearray() if self.version >= 4: result.append(self.version) result.append(self.signature_type) if self.version < 4: result.extend(utils.int_to_4byte( time.mktime(self.creation_time.timetuple()) )) else: result.append(self.public_key_algorithm) result.append(self.hash_algorithm) hashed_subpacket_data = b''.join( map(bytes, self.hashed_subpackets)) hashed_subpacket_length = len(hashed_subpacket_data) result.extend(utils.int_to_2byte(hashed_subpacket_length)) result.extend(hashed_subpacket_data) result.append(self.version) result.append(255) result.extend(utils.int_to_4byte(hashed_subpacket_length + 6)) return result
def public_key_to_bytes(pubkey): result = bytearray([pubkey['version']]) result.extend(utils.int_to_4byte(pubkey['creation_time'])) if pubkey['version'] in (2, 3): result.extend(utils.int_to_2byte(pubkey['expiration_days'])) result.append(pubkey['pub_algorithm_type']) if pubkey['pub_algorithm_type'] in (1, 2, 3): result.extend(utils.int_to_mpi(pubkey['modulus'])) result.extend(utils.int_to_mpi(pubkey['exponent'])) elif pubkey['pub_algorithm_type'] == 17: result.extend(utils.int_to_mpi(pubkey['prime'])) result.extend(utils.int_to_mpi(pubkey['group_order'])) result.extend(utils.int_to_mpi(pubkey['group_gen'])) result.extend(utils.int_to_mpi(pubkey['key_value'])) elif pubkey['pub_algorithm_type'] in (16, 20): result.extend(utils.int_to_mpi(pubkey['prime'])) result.extend(utils.int_to_mpi(pubkey['group_gen'])) result.extend(utils.int_to_mpi(pubkey['key_value'])) return result
def signature_to_bytes(signature): result = bytearray() sig_version = signature['sig_version'] result.append(sig_version) if sig_version >= 4: result.append(signature['sig_type']) result.append(signature['pub_algorithm_type']) result.append(signature['hash_algorithm_type']) hashed_subpacket_data = bytearray() unhashed_subpacket_data = bytearray() for sp in signature['subpackets']: subpacket_data = subpacket_to_bytes(sp) if sp['hashed']: hashed_subpacket_data.extend(subpacket_data) else: unhashed_subpacket_data.extend(subpacket_data) result.extend(utils.int_to_2byte(len(hashed_subpacket_data))) result.extend(hashed_subpacket_data) result.extend(utils.int_to_2byte(len(unhashed_subpacket_data))) result.extend(unhashed_subpacket_data) elif sig_version in (2, 3): result.append(0x05) result.append(signature['sig_type']) result.extend(utils.int_to_4byte(signature['creation_time'])) result.extend(signature['key_id']) result.append(signature['pub_algorithm_type']) result.append(signature['hash_algorithm_type']) result.extend(signature['hash2']) for value in signature['values']: if value is None: continue result.extend(utils.int_to_mpi(value)) return result
def make_key_expiration_time_subpacket(t, critical=False, hashed=True): data = utils.int_to_4byte(t) return make_signature_subpacket(9, data, critical, hashed)
def content(self): return utils.int_to_4byte(self.time)