简体   繁体   English

从ruby中的Torrent文件中提取哈希值

[英]Extract the hash from Torrent File in ruby

I was hoping to extract the hash identifier for a torrent file. 我希望提取torrent文件的哈希标识符。

Particularly, I'm looking for the same hash that shows up in Transmission/uTorrent upon opening up a torrent info dialogue (It looks like this: 7b435a6f051dec092a6ee440d793bfed6696cfa1) 特别是,我正在寻找在打开一个torrent信息对话框中显示在Transmission / uTorrent中的相同哈希(看起来像这样:7b435a6f051dec092a6ee440d793bfed6696cfa1)

I think that it's the SHA1 hash from the info dictionary on the torrent file. 认为这是来自torrent文件的信息词典的SHA1哈希。 If I were to parse over the binary file data from one byte to another byte, then perform a SHA1 hash encryption I could get it. 如果我要将二进制文件数据从一个字节解析为另一个字节,那么执行SHA1哈希加密我就可以得到它。

Does anyone have a better understanding or have some code that could do this? 有没有人有更好的理解或有一些代码可以做到这一点?

Using the bencode gem: 使用bencode gem:

require 'bencode'
require 'digest/sha1'

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

You could try RubyTorrent , there is an example of how to dump meta data from a .torrent file here: https://github.com/dydx/RubyTorrent/blob/master/dump-metainfo.rb 你可以试试RubyTorrent ,这里有一个如何从.torrent文件转储元数据的例子: https//github.com/dydx/RubyTorrent/blob/master/dump-metainfo.rb

There is also a bencode gem that can be used to parse .torrent files. 还有一个bencode gem可用于解析.torrent文件。

Using the firecracker gem 使用鞭炮宝石

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM