-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk_hash.py
More file actions
48 lines (37 loc) · 1.25 KB
/
chunk_hash.py
File metadata and controls
48 lines (37 loc) · 1.25 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
import os
import hashlib
import bencodepy
# Chunk size in bytes
Chunk_size = 1024 * 1024
# Function to chunk a file and create a .torrent file
def chunk_file(file_path):
file_size = os.path.getsize(file_path)
file_name = os.path.basename(file_path)
chunk_hashes = []
with open(file_path, 'rb') as f:
chunk_index = 0
while chunk := f.read(Chunk_size):
hash_obj = hashlib.sha1(chunk)
chunk_hashes.append(hash_obj.digest())
print(f"Hash of chunk {chunk_index} (SHA-1): {hash_obj.hexdigest()}")
chunk_index += 1
if not chunk_hashes:
print("Error: No chunks were hashed.")
return
info = {
'length': file_size,
'name': file_name,
'piece length': Chunk_size,
'pieces': b''.join(chunk_hashes),
}
torrent_data = {
'announce': 'http://127.0.0.1:9000',
'info': info,
}
torrent_file = file_path + '.torrent'
with open(torrent_file, 'wb') as f:
f.write(bencodepy.encode(torrent_data))
print(f"Created .torrent file: {torrent_file}")
# Input the path to the file you want to chunk
file_path = input("Enter the path to the file you want to chunk: ")
chunk_file(file_path)