Friday, 29 October 2010

Calculating magnet link using Ruby

It took me a couple of days to get this right even though it's really simple. The problem was that I didn't notice Some clients require Base32 of info_hash in magnet link specs. I'm hoping this will save at least a couple of hours for someone.

content = BEncode.load(file)
info_bencoded = content['info'].bencode
info_hash = Digest::SHA1.digest(info_bencoded)
info_hash32 = Base32.encode(info_hash)
info_hash32_escaped = URI.encode(info_hash32)
puts "magnet:?xt=urn:btih:#{info_hash32_escaped}"

This code requires bencode and base32 gems.

No comments: