2017-09-03 6 views
1
아래

코드 (추에 대한 사과), 나는이 급류에 대한 해시를 계산하기 위해 실행하는거야하지만 전송에 직접 그 토런트를 열 때 그것은 나에게와 다른 대답을주고있다 :Python3 계산 토런트 해시

I을 이 페이지에 r_000에서 테스트 해요 : http://gen.lib.rus.ec/repository_torrent/

전송은 나에게 제공합니다 63a04291a8b266d968aa7ab8a276543fa63a9e84

내 코드가 나에게 제공합니다 1882ff6534ee4aa660e2fbf225c1796638bea4c0

import bencoding 
from io import BytesIO 
import binascii 
import hashlib 

with open("cache/r_000.torrent", "rb") as f: 
    data = bencoding.bdecode(f.read()) 
info = data[b'info'] 
hashed_info = hashlib.sha1(info[b'pieces']).hexdigest() 
print(hashed_info) 

내가 망쳤다는 아이디어가 있습니까? 감사!

+0

. [이 대답] (https://stackoverflow.com/a/28162042/3151902)을보십시오. – user3151902

+0

'info'-dictionary 대신'pieces'-value를 해싱 한 것 같습니다. – Encombe

+0

그래, 한 걸음 물러서서 다시보아야했습니다. 좋아, 솔루션은 전체 정보 사전을 bencode 한 다음 해시하는 것입니다. –

답변

1

같은 실수를했습니다. 검색을 통해이 질문을 발견하면 해결할 수있었습니다. 그러나 python3에서 작업을 수행하는 방법에 대한 검색을 통해이 길을 걸어 다른 사람이 명확하게하기 +이 명시 적으로 수정입니다 :

변경 :

hashed_info = hashlib.sha1(info[b'pieces']).hexdigest() 

에 :

hashed_info = hashlib.sha1(bencoding.bencode(info)).hexdigest() 

여기에 정보 해시를 명확히하기위한 Encombe 덕분에 : https://stackoverflow.com/questions/28140766/28162042#28162042

마그넷 URI에서 찾은 토런트 클라이언트 또는 해시의 해시는 토런트 파일의 원시 bencoded 정보 사전의 부분 인 SHA1 해시입니다.


전체하지만 최소한의 예는 다음과 같습니다

import bencoding, hashlib 

objTorrentFile = open("r_0000.torrent", "rb") 
decodedDict = bencoding.bdecode(objTorrentFile.read()) 

info_hash = hashlib.sha1(bencoding.bencode(decodedDict[b"info"])).hexdigest() 
print(info_hash) 

결과 :이 이미 요청 된

$ python3 example.py 
63a04291a8b266d968aa7ab8a276543fa63a9e84 
+0

좋은 해결책이지만, 드문 경우지만 Bdecoding 그런 다음 해싱하기 전에 Bencoding하면 [잘못된 정보 _ 해시] (https://stackoverflow.com/questions/19749085/calculating-the-info-hash-of-a-torrent-file/19800109#19800109)가 표시 될 수 있습니다. – Encombe

+0

추가 정보를 제공해 주셔서 감사합니다. 그런 사건을 어떻게 막을 수 있습니까? 잘못된 순서와 디코더 라이브러리가 정렬되어서 불일치하거나 다른 경우가있을 때입니까? 내가 사용하고있는 라이브러리는 실제로 [bencoder] (https://github.com/utdemir/bencoder) 라이브러리이며 정렬 할 수있는 부분을 볼 수 있습니다. – CGar