From 1b687d0e1e3f6e18a36ecdf7cd35c318c7b46879 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 18 Dec 2013 20:45:21 -0800 Subject: [PATCH] Add packets_from_bytes to help parse multi-packet streams --- gpg-migrate.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) 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__': -- 2.26.2