Last active
April 5, 2024 14:51
-
-
Save Cryolitia/84cb2eaac3be13b7e22a211703c39efa to your computer and use it in GitHub Desktop.
sig2dot.py
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
# Visualize GnuPG Trust Web | |
# usage: gpg --list-sigs | python3 ./sig2dot.py | |
import fileinput | |
import re | |
import networkx | |
from matplotlib import pyplot | |
lines = [i for i in fileinput.input() if not i.startswith('sub')] | |
# lines = [i for i in open("gpg.txt", "r").readlines() if not i.startswith('sub')] | |
lines.reverse() | |
regex_uid = re.compile(r"(?<=\d{4}-\d{2}-\d{2}).*$") | |
regex_uid2 = re.compile(r"(?<=\]).*$") | |
public = dict() | |
while lines: | |
signers = set() | |
breakers = set() | |
line = lines.pop() | |
while not line.startswith('uid'): | |
line = lines.pop() | |
uid = regex_uid2.search(line).group().strip() | |
while True: | |
line = lines.pop() | |
if line.startswith('uid') and 'jpeg image' not in line: | |
if uid not in line: | |
result = regex_uid2.search(line).group().strip() | |
signers.add(result) | |
elif line.startswith('sig'): | |
if '用户标识未找到' not in line and '自签名' not in line and 'self-signature' not in line and 'User ID not found' not in line: | |
if uid not in line: | |
result = regex_uid.search(line).group().strip() | |
signers.add(result) | |
elif line.startswith('rev'): | |
result = regex_uid.search(line).group().strip() | |
breakers.add(result) | |
else: | |
break | |
public[uid] = [signers, breakers] | |
G = networkx.MultiDiGraph() | |
for key, items in public.items(): | |
for signer in items[0]: | |
G.add_edge(signer, key, color='k') | |
for breaker in items[1]: | |
G.add_edge(breaker, key, color='r') | |
colors = networkx.get_edge_attributes(G, 'color').values() | |
pos = networkx.spring_layout(G) | |
networkx.draw(G, pos, with_labels=True, node_size=2000, edge_color=colors) | |
pyplot.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
original from https://dustri.org/b/visualising-gpg-web-of-trust-with-python.html