-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlite3_crypto.py
More file actions
42 lines (26 loc) · 910 Bytes
/
sqlite3_crypto.py
File metadata and controls
42 lines (26 loc) · 910 Bytes
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
import binascii
import base64
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
class CryptoBot(object):
@staticmethod
def generate_key(password):
if len(password) >= 8:
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(password)
return base64.urlsafe_b64encode(digest.finalize())
return "Password Length less than 8"
@staticmethod
def encrypt(message, key):
key_token = Fernet(key)
cipher_text = key_token.encrypt(message)
return cipher_text
@staticmethod
def decrypt(cipher_text, key):
key_token = Fernet(key)
return key_token.decrypt(bytes(cipher_text))
#key = CryptoBot.generate_key("How are you!")
#message = "Fine. Thank you!"
#print CryptoBot.encrypt(message, key)
#print CryptoBot.decrypt(CryptoBot.encrypt(message, key), key)