-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencryptMessage.py
86 lines (78 loc) · 2.59 KB
/
encryptMessage.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
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
from cryptography.fernet import Fernet
import json
import os
from store import Store
import datetime
import hashlib
from emoji import EmojiConverter
import zlib
def generate_hash(encrypted) -> str:
# generate sha256 hash for text
return hashlib.sha256(encrypted).hexdigest()
class Encryptor(object):
"""
The Encryptor object, handles encryption and decryption
"""
def __init__(self):
# process mongodb environs
try:
self.dbUser = os.environ.get("API_USER")
self.dbPw = os.environ.get("API_PASSWORD")
except:
self.dbUser = 0
self.dbPw = 0
self.dbUrl = os.environ.get("API_URL")
# Add store object to interact with the database
self.st = Store(self.dbUrl, self.dbUser, self.dbPw)
# Emoji converter
self.conv = EmojiConverter("emojList.txt")
def encrypt(self, message, emoji=False)->str:
# Use fernet to generate key
key = Fernet.generate_key()
f = Fernet(key)
# Compress the intended message via zlib
encoded = zlib.compress(message.encode())
# The encryption
encrypted = f.encrypt(encoded)
# Generate the hash of the encrypteds tring
hsh = generate_hash(encrypted)
# decoded key to store in mongodb
_strkey = key.decode()
# Store data in mongodb
doc = {
"hsh": hsh,
"key": _strkey,
"destroy": False,
"compress": "zlib",
"date": datetime.datetime.utcnow(),
}
self.st.add(doc)
# Return the encrypted message
encrypted = encrypted.decode()
if emoji:
encrypted = self.conv.sentence_to_emoji(encrypted)
return encrypted
def decrypt(self, encryptedMessage, destroy=False)->str:
# hashing the message
em = encryptedMessage.decode()
if self.conv.is_emoji(em[0]):
encryptedMessage = self.conv.emoji_to_sentence(em).encode()
hsh = generate_hash(encryptedMessage)
# Find the dataset in mongodb
if destroy:
d = self.st.desFind({"hsh": hsh})
else:
d = self.st.find({"hsh": hsh})
if d == None:
return -1
# decrypt with key
key = d["key"].encode()
f = Fernet(key)
_msg = encryptedMessage
res = f.decrypt(_msg)
res = zlib.decompress(res).decode()
return res
def destroy(self, encryptedMessage) -> bool:
hsh = generate_hash(encryptedMessage)
fnd = self.st.desFind({"hsh": hsh})
return fnd != {}