Created
October 23, 2017 02:27
-
-
Save tomholub/9b45a8443c75ac30bd3f9cb2e410653d to your computer and use it in GitHub Desktop.
Parsing SKS PGP Key Dump in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openpgp | |
from datetime import datetime | |
# from validate_email import validate_email | |
pubkeys = [] | |
with open('/home/james/Desktop/sks/sks-dump-0000.pgp', 'rb') as fp: | |
s = datetime.now() | |
packets = openpgp.OpenPGPFile(fp) | |
print((datetime.now() - s).total_seconds()) | |
pubkey = {} | |
subkey = {} | |
skip = False | |
for packet in packets: | |
try: | |
if packet['tag_name'] == 'Public-Key': | |
if pubkey: | |
pubkeys.append(pubkey) | |
if 'fingerprint' in packet: | |
pubkey = {"fingerprint": packet['fingerprint'], "created": packet['creation_time'], "users": [], "subkeys": []} | |
skip = False | |
else: | |
pubkey = {} | |
skip = True | |
elif skip: | |
pass | |
elif packet['tag_name'] == 'User ID': | |
pubkey['users'].append(packet['user_id']) | |
elif packet['tag_name'] == 'Signature': | |
# may follow public subkey or public key | |
pass # check key expiration + can encrypt + can sign | |
elif packet['tag_name'] == 'Public-Subkey': | |
pubkey['subkeys'].append({'fingerprint': packet['fingerprint'], 'created': packet['creation_time']}) | |
elif packet['tag_name'] == 'User Attribute': | |
pass | |
else: | |
print(packet) | |
raise('unknown packet') | |
except KeyError: | |
print(packet) | |
raise | |
pubkeys.append(pubkey) | |
print((datetime.now() - s).total_seconds()) | |
print(len(pubkeys)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment