1+ #!/usr/bin/env python3
2+
3+ from hashlib import sha256
4+ import sys
5+
6+ def content_hash (content :str ):
7+ return sha256 (content .encode ('utf-8' )).hexdigest ()
8+
9+ def read_file (filename ):
10+ with open (filename , 'r' ) as f :
11+ data = f .readlines ()
12+ return data
13+
14+ def regenerate_hash (filename ):
15+ data = read_file ("createhackenv.sh" )
16+ script_hash_line = data [- 1 ]
17+ # print(f"Script hash line: {script_hash_line}")
18+ raw_script_hash = script_hash_line .split (" " )[3 ].strip ()
19+ # print(f"Script hash in Script: {raw_script_hash}")
20+ content = "" .join (data )
21+ content = content .replace (raw_script_hash , "__HASH__" )
22+ calc_script_hash = content_hash (content )
23+ # print(f"Script reCalc hash: {calc_script_hash}")
24+ # print(f"Script hash: {raw_script_hash}")
25+ return calc_script_hash , raw_script_hash
26+
27+ def verify_hash (calc_script_hash , raw_script_hash ):
28+ print ("Verifying script hash..." )
29+ print (f"Script reCalc hash: { calc_script_hash } " )
30+ print (f"Script hash wait for check: { raw_script_hash } " )
31+ if calc_script_hash == raw_script_hash :
32+ print ("Script hash is correct" )
33+ else :
34+ print ("Script hash is incorrect!" )
35+ exit (114 )
36+
37+ def main ():
38+ if len (sys .argv ) == 2 :
39+ regen_hash ,raw_hash = regenerate_hash (sys .argv [1 ])
40+ verify_hash (regen_hash , raw_hash )
41+ elif len (sys .argv ) == 3 :
42+ user_hash = sys .argv [2 ]
43+ regen_hash ,raw_hash = regenerate_hash (sys .argv [1 ])
44+ verify_hash (regen_hash , user_hash )
45+ else :
46+ print ("Usage: python3 verify.py <filename> [<check_hash>]" )
47+ exit (1 )
48+
49+ if __name__ == "__main__" :
50+ main ()
0 commit comments