2011-06-27 38 views
4

我曾希望提取一個torrent文件的哈希標識符。在ruby中從Torrent文件中提取哈希

特別,我正在尋找在開放洪流信息對話框(它看起來像這樣:7b435a6f051dec092a6ee440d793bfed6696cfa1),它顯示了在傳輸/ uTorrent的相同的散列

認爲,它是從SHA1哈希torrent文件中的信息詞典。如果我要解析二進制文件數據從一個字節到另一個字節,然後執行SHA1哈希加密,我可以得到它。

有沒有人有更好的理解或有一些代碼可以做到這一點?

回答

2

使用bencode寶石:在一些文檔1點:

require 'bencode' 
require 'digest/sha1' 

meta = BEncode.load_file(file) # File or file path 
info_hash = Digest::SHA1.hexdigest(meta["info"].bencode) 
+0

你的回答很好地工作,但是你可以用兩件事情更新找你的答案它聲明這個散列是torrent文件的整個信息部分的SHA1散列。 2 - 爲什麼你從bencoded信息獲取SHA1,而不是來自bdecoded? – SET

+1

@SET沒有任何,它似乎是一個普遍採用的約定,不是實際的位洪流規範的一部分。這就是爲什麼你不讀取洪流本身的信息散列。信息哈希很簡單,就是洪流的「信息」部分的SHA1哈希(原樣;未解碼)。 「info」部分封裝了能夠唯一標識一個torrent的所有內容([docs](https://wiki.theory.org/BitTorrentSpecification#Metainfo_File_Structure))。更多信息:http://ktorrent.org/wiki/index.php/Info_hash –

0

使用firecracker寶石

require "firecracker" 
require "bencode_ext" 
require 'open-uri' 

torrent = open(link).read 

# Get the info_hash from torrent file 
info_hash = Firecracker.hash(torrent.bdecode) 
puts "Info Hash = " + info_hash