File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import hashlib
2+ import random
3+
4+
5+ def modify_file_byte (file_path ):
6+ with open (file_path , 'rb' ) as file :
7+ data = bytearray (file .read ())
8+
9+ # Generate a random index to modify
10+ index = random .randint (0 , len (data ) - 1 )
11+
12+ # Generate a random byte to replace the existing byte
13+ new_byte = random .randint (0 , 255 )
14+
15+ # Modify the byte at the randomly chosen index
16+ data [index ] = new_byte
17+
18+ # Write the modified data to a new file
19+ modified_file_path = 'modified-' + file_path
20+ with open (modified_file_path , 'wb' ) as modified_file :
21+ modified_file .write (data )
22+
23+ return modified_file_path
24+
25+
26+ def calculate_hash (file_path ):
27+ with open (file_path , 'rb' ) as file :
28+ data = file .read ()
29+ hash_obj = hashlib .sha256 (data )
30+ return hash_obj .hexdigest ()
31+
32+
33+ # Example usage
34+ input_file_path = 'example.jpg' # Replace with the path to your input file
35+
36+ original_hash = calculate_hash (input_file_path )
37+ print (f'Original Hash: { original_hash } ' )
38+
39+ modified_file_path = modify_file_byte (input_file_path )
40+ modified_hash = calculate_hash (modified_file_path )
41+ print (f'Modified Hash: { modified_hash } ' )
You can’t perform that action at this time.
0 commit comments