torrent_parser icon indicating copy to clipboard operation
torrent_parser copied to clipboard

add functions to calculate v1 and v2 info hashes of torrent files

Open milahu opened this issue 10 months ago • 3 comments

currently the v1 hash appears only in this test

https://github.com/7sDream/torrent_parser/blob/23b9e110beb5b91c5498b286bd9d8cce83cfc076/tests/test_info_hash.py#L15-L20

expected:

torrent = torrent_parser.parse_torrent_file("input.torrent", hash_raw=True) 

info_hash_v1_raw = torrent_parser.get_info_hash_v1_raw(torrent) # -> bytes
info_hash_v1_hex = torrent_parser.get_info_hash_v1_hex(torrent) # -> string

info_hash_v2_raw = torrent_parser.get_info_hash_v2_raw(torrent) # -> bytes
info_hash_v2_hex = torrent_parser.get_info_hash_v2_hex(torrent) # -> string

get_info_hash_v2 simply uses hashlib.sha256 instead of hashlib.sha1

binascii.hexlify can be avoided by using hexdigest

info_hash_v1 = hashlib.sha1(info_bytes).hexdigest()
info_hash_v2 = hashlib.sha256(info_bytes).hexdigest()

related: https://stackoverflow.com/questions/46025771/python3-calculating-torrent-hash


stupid question: does parse_torrent_file preserve the sort order of the info dict? since python3, dict should be an ordered dict by default

https://stackoverflow.com/questions/19749085/calculating-the-info-hash-of-a-torrent-file

Be observant that the example torrent file given by Arvid, both the root-dictionary and the info-dictionary is unsorted. According to the bencode specification a dictionary must be sorted. However the agreed convention when a info-dictionary for some reason is unsorted, is to hash the info-dictionary raw as it is (unsorted), as explained by Arvid above.

milahu avatar Sep 02 '23 19:09 milahu