-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileDataChecker.py
129 lines (109 loc) · 4.59 KB
/
fileDataChecker.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import hashlib
from pymongo import MongoClient
from tkinter import *
from tkinter import filedialog
import os
class FileDataChecker:
def __init__(self,filename,key):
self.filename = filename
self.key = key
def readFileData(self):
file = open(self.filename,"rb")
self.fileData = file.read()
file.close()
def getHasherData(self):
client = MongoClient("mongodb+srv://subhra:<MONGODB KEY>@cluster0.stksg.mongodb.net/change_detector?retryWrites=true&w=majority")
db = client['change_detector']
hasher_data = db['hasher_data']
x = hasher_data.find_one({"_id":self.key})
if(x==None):
return False
self.salt = x["salt"]
self.hashArray = x["hashedDataArray"]
return True
def hasher(self):
self.editedFileHashArray = []
self.editedFileUnhashedDataArray = []
start = 0
end = min(len(self.fileData),3)
while(start<len(self.fileData)):
if(end == len(self.fileData)+1):
break
substr = self.fileData[start:end]
self.editedFileUnhashedDataArray.append(substr)
result = hashlib.sha256(substr+self.salt).digest()
self.editedFileHashArray.append(result)
start +=1
end +=1
def compareData(self,T):
oriLen = len(self.hashArray)
modLen = len(self.editedFileHashArray)
comparedResult = ""
extraText=""
dataUnchanged = True
T.tag_configure("highlight",foreground="#FF0000")
if(oriLen == modLen):
if(self.hashArray[0] == self.editedFileHashArray[0]):
T.insert(END,"{}".format(str(self.editedFileUnhashedDataArray[0],"utf-8")))
else:
T.insert(END,"{}".format(str(self.editedFileUnhashedDataArray[0],"utf-8")),"highlight")
i = 1
while(i<oriLen):
if(self.hashArray[i] != self.editedFileHashArray[i]):
# highlight this text
temp = str(self.editedFileUnhashedDataArray[i],"utf-8")
T.insert(END,temp[-1],"highlight")
dataUnchanged = False
else:
temp = str(self.editedFileUnhashedDataArray[i],"utf-8")
T.insert(END,temp[-1])
i+=1
elif(oriLen > modLen):
if(self.hashArray[0] == self.editedFileHashArray[0]):
T.insert(END,"{}".format(str(self.editedFileUnhashedDataArray[0],"utf-8")))
else:
T.insert(END,"{}".format(str(self.editedFileUnhashedDataArray[0],"utf-8")),"highlight")
i = 1
dataUnchanged = False
while(i<modLen):
if(self.hashArray[i] != self.editedFileHashArray[i]):
#highlight this text
temp = str(self.editedFileUnhashedDataArray[i],"utf-8")
T.insert(END,temp[-1],"highlight")
else:
temp = str(self.editedFileUnhashedDataArray[i],"utf-8")
T.insert(END,temp[-1])
i+=1
else: # modLen > orilen
if(self.hashArray[0] == self.editedFileHashArray[0]):
T.insert(END,"{}".format(str(self.editedFileUnhashedDataArray[0],"utf-8")))
else:
T.insert(END,"{}".format(str(self.editedFileUnhashedDataArray[0],"utf-8")),"highlight")
i = 1
dataUnchanged = False
while(i<oriLen):
if(self.hashArray[i] != self.editedFileHashArray[i]):
#highlight this text
temp = str(self.editedFileUnhashedDataArray[i],"utf-8")
T.insert(END,temp[-1],"highlight")
else:
#highlight this text
temp = str(self.editedFileUnhashedDataArray[i],"utf-8")
T.insert(END,temp[-1])
i+=1
i = oriLen
while(i<modLen):
temp = str(self.editedFileUnhashedDataArray[i],"utf-8")
T.insert(END,temp[-1],"highlight")
i+=1
message=""
if(dataUnchanged):
message = "The Data in the two files is unchanged"
else:
if(oriLen==modLen):
message="The edited data is marked in red color"
elif(oriLen<modLen):
message="Some data is extra in the Edited file"
else:
message="Some data is missing from the Edited file"
return message