Skip to content

Commit 2f2569f

Browse files
authored
uploaded python script
1 parent 6439a9d commit 2f2569f

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Change_Hashcode_for_image.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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}')

0 commit comments

Comments
 (0)