From: W. Trevor King Date: Thu, 19 Dec 2013 04:45:21 +0000 (-0800) Subject: Add packets_from_bytes to help parse multi-packet streams X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1b687d0e1e3f6e18a36ecdf7cd35c318c7b46879;p=gpg-migrate.git Add packets_from_bytes to help parse multi-packet streams --- diff --git a/gpg-migrate.py b/gpg-migrate.py index 00e672c..d227be7 100755 --- a/gpg-migrate.py +++ b/gpg-migrate.py @@ -58,6 +58,14 @@ class PGPPacket (dict): pass +def packets_from_bytes(data): + offset = 0 + while offset < len(data): + packet = PGPPacket() + offset += packet.from_bytes(data=data[offset:]) + yield packet + + def migrate(old_key, new_key): """Add the old key and sub-keys to the new key @@ -67,16 +75,16 @@ def migrate(old_key, new_key): """ old_key_export = _get_stdout( ['gpg', '--export', old_key]) - old_key_packet = PGPPacket() - old_key_packet.from_bytes(data=old_key_export) + old_key_packets = list( + packets_from_bytes(data=old_key_export)) old_key_secret_export = _get_stdout( ['gpg', '--export-secret-keys', old_key]) - old_key_secret_packet = PGPPacket() - old_key_secret_packet.from_bytes(data=old_key_secret_export) + old_key_secret_packets = list( + packets_from_bytes(data=old_key_secret_export)) import pprint - pprint.pprint(old_key_packet) - pprint.pprint(old_key_secret_packet) + pprint.pprint(old_key_packets) + pprint.pprint(old_key_secret_packets) if __name__ == '__main__':