-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataVerifier.py
More file actions
70 lines (55 loc) · 1.6 KB
/
Copy pathdataVerifier.py
File metadata and controls
70 lines (55 loc) · 1.6 KB
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
import hashlib
import sys
from importlib.resources import path
from pymongo import MongoClient
#function definition
"""
This function stores the hash
to the mongoDB database
"""
def check_hash(fileHash):
try:
client = MongoClient("mongodb://127.0.0.1:27017")
except:
print("Could not connect to MongoDB")
# Access database
mydatabase = client['hash'] # If not already created, a new database will be created
# Access collection of the database
mycollection=mydatabase['files']
item_details = mycollection.find()
val = False
for item in item_details:
if(item['Hash'] == fileHash):
val = True
if(val):
global hashMatches
hashMatches += file + "\n"
else:
global hashDoesnotMatch
hashDoesnotMatch += file + "\n"
client.close()
#function definition
"""
This function generates hash for given file
"""
def read_file(file_path):
with open(file_path, 'rb') as f:
for byte_block in iter(lambda: f.read(4096),b""):
sha256_hash.update(byte_block)
path = sys.argv[1]
file = None
hashMatches = ""
hashDoesnotMatch = ""
sha256_hash = None
with open(path, encoding='utf8') as f:
for line in f:
file = line.strip()
sha256_hash = hashlib.sha256()
read_file(file)
check_hash(sha256_hash.hexdigest())
if(len(hashDoesnotMatch) == 0):
print("Hash Matches for all files")
else:
print("Hash Matches for files: \n", hashMatches)
if(len(hashDoesnotMatch) > 0):
print("Hashes do not match for files: \n", hashDoesnotMatch)