This repository was archived by the owner on Jul 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsecret.py
More file actions
52 lines (33 loc) · 1.27 KB
/
secret.py
File metadata and controls
52 lines (33 loc) · 1.27 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
from nacl.public import PrivateKey, SealedBox
from nacl.signing import SigningKey
from nacl.secret import SecretBox
from nacl.utils import random
from os.path import exists
from config import nacl_sk_path
if not exists(nacl_sk_path):
sk = PrivateKey.generate()
sk_raw = sk.encode()
with open(nacl_sk_path, 'wb') as f:
f.write(sk_raw)
else:
with open(nacl_sk_path, 'rb') as f:
sk_raw = f.read()
def encrypt(plaintext: bytes):
return SealedBox(PrivateKey(sk_raw).public_key).encrypt(plaintext)
def decrypt(ciphertext: bytes):
return SealedBox(PrivateKey(sk_raw)).decrypt(ciphertext)
def sign(message: bytes):
return SigningKey(sk_raw).sign(message).signature
def verify(message: bytes, signature: bytes):
return SigningKey(sk_raw).verify_key.verify(message, signature)
def new_symmetric_key():
return random(SecretBox.KEY_SIZE)
def symmetric_encrypt(symmetric_key: bytes, plaintext: bytes):
return SecretBox(symmetric_key).encrypt(plaintext)
def symmetric_decrypt(symmetric_key: bytes, ciphertext: bytes):
return SecretBox(symmetric_key).decrypt(ciphertext)
def get_pk_raw():
return PrivateKey(sk_raw).public_key.encode()
def new_pair():
sk = PrivateKey.generate()
return sk.encode(), sk.public_key.encode()