-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash-generator.py
36 lines (30 loc) · 1.12 KB
/
hash-generator.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
import hashlib
from os import getcwd, listdir, walk
from os.path import dirname, expanduser, join, normpath
from pathlib import Path
# First get the root
project_root = "get root via manually entering here or through os or through some other utils"
def get_files():
current_path = project_root.joinpath('cpp')
files_to_hash = []
for root, dirs, files in walk(current_path):
files_to_hash = [join(root, file) for root, dirs, files in walk(expanduser(current_path)) for file in files]
return files_to_hash
def generate_hashes():
files = get_files()
hashes = []
for each_file in files:
# choose hash algorithm here
hasher = hashlib.sha256()
with open(each_file, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
hashes.append('{}\t{}\n'.format(each_file.replace(normpath(project_root),''), hasher.hexdigest()))
return hashes
def save_hashes():
hashes = generate_hashes()
with open('hash-list.txt', 'w') as f:
for hash in hashes:
f.write(hash)
if __name__ == '__main__':
save_hashes()