-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswordSave.py
79 lines (67 loc) · 2.69 KB
/
passwordSave.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
import os
import time
from pathlib import Path
from Crypto.PublicKey import RSA
from Crypto.Util import asn1
from base64 import b64decode
import pickle
statusDir = Path("/tmp/status404")
if(statusDir.is_dir()):
pass
else:
os.mkdir("/tmp/status404")
print("passwordSave functionality starting")
customerName = input("Enter the customer name: ")
deviceName = input("Enter the device name: ")
customerDevicePass = input("Enter the customer password: ")
# TO BE FETCHED FROM THE DATABASE
# DEVICE NAME CAN BE DEVICE IP + PORT
passwordName = customerName + "-" + deviceName
passwordLocation = "/passwordDatabase/"
baseDir = Path(passwordLocation)
print("passwordLocation = " + passwordLocation)
if(baseDir.is_dir()):
print("Directory exists, no need to create the passwordLocation")
else:
print("Creating passwordLocation")
os.mkdir(passwordLocation)
passwordFile = Path(passwordLocation + passwordName)
passwordPath = passwordLocation + passwordName
print("passwordFile = " + passwordPath)
if(passwordFile.is_file()):
print("passwordFile already exists, skipping the creation operation, please delete the key or perform the update operation if you want to update")
print("WARNING: DELETION OF KEY MAY LEAD TO ADVERSE EFFECTS.")
os.system("echo '1' > /tmp/status404/keygenStatus")
exit(1)
else:
print("File doesn't exists")
print("password encryption started")
passwordKeyName = customerName + "-" + deviceName
passwordKeyLocation = "/passwordKeyDatabase/"
passwordKeyDir = Path(passwordKeyLocation)
print("passwordKeyLocation = " + passwordKeyLocation)
if(passwordKeyDir.is_dir()):
print("Directory exists, no need to create the passwordKeyLocation")
else:
print("Creating passwordKeyLocation")
os.mkdir(passwordKeyLocation)
passwordKeyFile = Path(passwordKeyLocation + passwordKeyName + ".pem")
passwordKeyPath = passwordKeyLocation + passwordKeyName + ".pem"
print("passwordKeyFile = " + passwordKeyPath)
if(passwordKeyFile.is_file()):
print("passwordKeyFile already exists, skipping the creation operation, please delete the key or perform the update operation if you want to update")
print("WARNING: DELETION OF KEY MAY LEAD TO ADVERSE EFFECTS.")
os.system("echo '1' > /tmp/status404/keygenStatus")
exit(1)
else:
print("File doesn't exists")
print("password encryption started")
passwordKey = RSA.generate(2048)
privateKey = passwordKey.exportKey('PEM')
print(type(privateKey) )
publicKey = passwordKey.publickey()
encryptedDevicePassword = publicKey.encrypt(customerDevicePass.encode('utf-8'),32)
with open(passwordPath, 'wb') as f:
pickle.dump(encryptedDevicePassword, f)
with open(passwordKeyPath, 'wb') as file:
file.write(privateKey)