-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyring.py
More file actions
156 lines (135 loc) · 4.53 KB
/
keyring.py
File metadata and controls
156 lines (135 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#
# A database of RSA public keys
# Which is the core of Plexus, in its way.
#
# Each row in the database has three fields: Twitter screen name, Twitter id, and the public key (JSON-ized)
#
import os, sqlite3, json, logging
import rsa
# Starter entries for the database of public keys
#
e1 = {"uname": "cryptw__t", "id": "1", "pubkey": '''-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAi/Tc1XAIO0VsY0VDbuuyqVvSLDBebpihOi4B0gvF8KG5Wfw5EGTh
xoRY4plleE3mZcy1UxGhu970CT5XoM2gRaisXN61/RC+YmQF6QyVdQHkRHxMzHI1
41avoW0AiUNFkvfDDKqB0VQHWPfDXJ7pcWELEBPNJrzn2vxUEJwx04Ztgo2ehMYc
AUiPG2HPqlFjtnlMsdI4MqTADvqnSlOppSdpJ3Z37dMmicL7MFPmeL1BOJ152/eQ
L+HMXQvwzJJrT8u5qff5r3MAC8pEKGqQO1Zcsf+T6J1y+Cm2qOKwk7QAF67YnZlF
Q+wTsBORiSjegKeq6K6aXVVQY3BMGk37bwIDAQAB
-----END RSA PUBLIC KEY-----'''}
# Generate the name of the keyfile
#
def get_db_name(is_usb=False):
fn = os.path.join(os.path.expanduser('~'),'.cryptweet-keys.db')
return fn
def get_key_by_name(name):
# Open the database and return a key matching a Twitter username
db_fn = get_db_name()
if os.path.isfile(db_fn):
conn = sqlite3.connect(db_fn, timeout=15) # open key database
c = conn.cursor()
c.execute("select * from keys where uname=?", (name,))
row = c.fetchone() # Get the first match -- should be the only one
if row == None: # No matches
retkey = None
else:
retkey = rsa.PublicKey.load_pkcs1(row[2],format='PEM')
c.close() # close key database
else:
logging.error("Key database does not exist!")
retkey = None
return retkey
def get_key_by_id(the_id):
# Open the database and return a key matching a Twitter user ID
db_fn = get_db_name()
if os.path.isfile(db_fn):
conn = sqlite3.connect(db_fn, timeout=15) # open key database
c = conn.cursor()
c.execute("select * from keys where id=?", (the_id,))
row = c.fetchone() # Get the first match -- should be the only one
if row == None: # No matches
retkey = None
else:
retkey = rsa.PublicKey.load_pkcs1(row[2],format='PEM')
c.close() # close key database
else:
logging.error("Key database does not exist!")
retkey = None
return retkey
#
# Add a key to the database
# Returns True if it worked, False otherwise
# If the key already exists, will put the updated copy into the database
#
def add_key(uname, the_id, pubkey):
# Open the database and return a key matching a Twitter user ID
db_fn = get_db_name()
if os.path.isfile(db_fn):
conn = sqlite3.connect(db_fn, timeout=15) # open key database
c = conn.cursor()
data = (uname, int(the_id), pubkey.save_pkcs1(format='PEM'))
try:
c.execute('select * from keys where uname=?', (uname,)) # Does record already exist?
row = c.fetchone()
if row != None:
c.execute('update keys SET id = ?, pubkey =? WHERE uname=?', (data[1], data[2], data[0])) # modify it
#print 'updating...'
else:
c.execute('insert into keys values (?,?,?)', data) # create new record
#print 'inserting...'
conn.commit() # Commit changes
retval = True
except:
retval = False
c.close() # close key database
else:
logging.error("Key database does not exist!")
retval = False
return retval
# Setup the key database from scratch
# If it already exists, complain and do nothing
# Use one seed key - @cryptw__t - to start things off
#
def setup_key_db():
db_fn = get_db_name()
if os.path.isfile(db_fn):
print "Key database already exists, not initializing. Delete it & run again to reinitialize it."
print "Dumping the key database"
conn = sqlite3.connect(db_fn)
c = conn.cursor()
c.execute('select * from keys order by uname')
print
print "Database contents:"
print
for row in c:
print row
c.close()
else:
conn = sqlite3.connect(db_fn)
# Now create the table
c = conn.cursor()
c.execute('''create table keys (uname text primary key, id integer, pubkey text)''')
if add_key(e1['uname'], e1['id'], rsa.PublicKey.load_pkcs1(e1['pubkey'], format='PEM')) == False:
print 'Failed add_key'
# Second test to see if modification works
if add_key(e1['uname'], e1['id'], rsa.PublicKey.load_pkcs1(e1['pubkey'], format='PEM')) == False:
print 'Failed UPDATE add_key'
# Dump the table to check to see if everything is alright
c.execute('select * from keys order by uname')
print
print "Database contents:"
print
for row in c:
print row
# And run a quick test
n = ("cryptw__t",)
c.execute('select * from keys where uname=?', n)
print
print "Search results on %s:" % n
print
for row in c:
print row
c.close()
# When run from the command line, it sets up the database file
#
if __name__ == '__main__':
setup_key_db()