Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parsing SKS PGP Key Dump in Python
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
You can’t perform that action at this time.