-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.py
57 lines (37 loc) · 1.03 KB
/
write.py
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
import base64
import uuid
import getpass
import nfc.snep
import ndef
from nacl.public import PrivateKey, Box
uid = str(uuid.uuid4())
passphrase = getpass.getpass('Passphrase: ')
print('Generating keypair')
prv = PrivateKey.generate()
pub = prv.public_key
box = Box(prv, pub)
print('Encrypting')
encrypted = box.encrypt(passphrase.encode())
print('Storing key')
with open('./keys/%s.pub' % uid, 'wb') as writer:
writer.write(bytearray(pub.encode()))
with open('./keys/%s.key' % uid, 'wb') as writer:
writer.write(bytearray(encrypted))
print('Disposing unnecessary data')
del passphrase
del pub
print('Waiting for NFC tag')
clf = nfc.ContactlessFrontend('usb')
tag = clf.connect(rdwr={'on-connect': lambda t: False})
print('Writing')
uid_record = ndef.TextRecord(uid)
uid_record.name = 'touchkey:uid'
prv_record = ndef.TextRecord(base64.b64encode(prv.encode()))
prv_record.name = 'touchkey:prv'
tag.ndef.records = [uid_record, prv_record]
print('Cleaning up')
del uid_record
del prv_record
del prv
del uid
print('All done!')