user748082
user748082

Reputation: 51

Extract the hash from Torrent File in ruby

I was hoping to extract the hash identifier for a torrent file.

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)

I think that it's the SHA1 hash from the info dictionary on the torrent file. 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.

Does anyone have a better understanding or have some code that could do this?

Upvotes: 5

Views: 2479

Answers (3)

Papaya Labs
Papaya Labs

Reputation: 1089

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

Upvotes: 0

Jorge Israel Peña
Jorge Israel Peña

Reputation: 38586

Using the bencode gem:

require 'bencode'
require 'digest/sha1'

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

Upvotes: 4

Theo
Theo

Reputation: 132862

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

There is also a bencode gem that can be used to parse .torrent files.

Upvotes: 1

Related Questions