Skip to content

Commit 9fa9a0c

Browse files
author
Chelsea Boling
authored
Create hash-generator.py
1 parent 3df5e98 commit 9fa9a0c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

hash-generator.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import hashlib
2+
from os import getcwd, listdir, walk
3+
from os.path import dirname, expanduser, join, normpath
4+
from pathlib import Path
5+
6+
# First get the root
7+
project_root = "get root via manually entering here or through os or through some other utils"
8+
9+
def get_files():
10+
current_path = project_root.joinpath('cpp')
11+
files_to_hash = []
12+
13+
for root, dirs, files in walk(current_path):
14+
files_to_hash = [join(root, file) for root, dirs, files in walk(expanduser(current_path)) for file in files]
15+
return files_to_hash
16+
17+
def generate_hashes():
18+
files = get_files()
19+
hashes = []
20+
for each_file in files:
21+
# choose hash algorithm here
22+
hasher = hashlib.sha256()
23+
with open(each_file, "rb") as f:
24+
for chunk in iter(lambda: f.read(4096), b""):
25+
hasher.update(chunk)
26+
hashes.append('{}\t{}\n'.format(each_file.replace(normpath(project_root),''), hasher.hexdigest()))
27+
return hashes
28+
29+
def save_hashes():
30+
hashes = generate_hashes()
31+
with open('hash-list.txt', 'w') as f:
32+
for hash in hashes:
33+
f.write(hash)
34+
35+
if __name__ == '__main__':
36+
save_hashes()

0 commit comments

Comments
 (0)