forked from elyukai/optolith-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryptTables.py
58 lines (48 loc) · 1.75 KB
/
encryptTables.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
import base64
import os
import os.path
import sys
import zipfile
import cryptography
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
def encryptTables(tableFolder):
# Zip files into project
zipf = zipfile.ZipFile('Tables.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir(tableFolder, zipf)
zipf.close()
# Obtain key from enviroment variable
key = os.environ['OPTOLITH_KEY']
# Create encryption key
salt = b'optolithSalt'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend())
cryptoKey = base64.urlsafe_b64encode(kdf.derive(key.encode(encoding='UTF-8')))
# Encrypt file
f = Fernet(cryptoKey)
with open("Tables.zip", "rb") as sourceFile:
with open("Tables.crypto", "wb") as targetFile:
encrypted = f.encrypt(sourceFile.read()) # For now. This creates larger files then necessary
# encryptedBytes = base64.decodebytes(encrypted)
targetFile.write(encrypted)
# Delete old table zip
os.remove("Tables.zip")
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
(p, extension) = os.path.splitext(file)
if extension == ".yaml" or extension == ".json" or extension == ".ts":
ziph.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), path))
if __name__ == "__main__":
if(len(sys.argv)) == 1:
path = input("Bitte Pfad zu den Tabellen eingeben:\n> ")
else:
path = sys.argv[1]
encryptTables(path)