0

How can I calculate the hash value of a torrent file using Java? Can I calculate it using bencode?

harto
  • 89,823
  • 9
  • 47
  • 61
Ramesh
  • 11
  • 1
  • 1

3 Answers3

6

Torrent files are hashed using SHA-1. You can use MessageDigest to get a SHA-1 instance. You need to read until 4:info is reached and then gather the bytes for the digest until remaining length minus one.

Note: This implementation works for most torrents, but the .torrent file is not guaranteed to end with the info key.

File file = new File("/file.torrent");
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
InputStream input = null;

try {
    input = new FileInputStream(file);
    StringBuilder builder = new StringBuilder();
    while (!builder.toString().endsWith("4:info")) {
        builder.append((char) input.read()); // It's ASCII anyway.
    }
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    for (int data; (data = input.read()) > -1; output.write(data));
    sha1.update(output.toByteArray(), 0, output.size() - 1);
} finally {
    if (input != null) try { input.close(); } catch (IOException ignore) {}
}

byte[] hash = sha1.digest(); // Here's your hash. Do your thing with it.
Zitrax
  • 19,036
  • 20
  • 88
  • 110
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • i tried bytes to convert to hash value but the hash value is different ... for (int i = 0; i < hash.length; i++) { System.out.print(Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1)); } – Ramesh Aug 09 '10 at 01:54
  • @Ramesh - perhaps the file got mangled when you saved it? Did you (perchance) use a `Reader` or `Scanner` to do that? Anyway, you haven't supplied enough information to allow someone else to figure out what is really going on. – Stephen C Aug 09 '10 at 02:50
  • @Ramesh: you're right, the hash should exist of only the `info` key of the torrent's dictionary. This is located at end of torrent file, bordered by `4:info` and `e`. I've updated the answer accordingly. – BalusC Aug 09 '10 at 04:21
  • ya it works fine now so i can also get the seeds ..Leechers and file size like this???? – Ramesh Aug 09 '10 at 04:27
  • 1
    There is no guarantee that the info-dict is at the very end of the torrent file. Mostly it is, but there are cases when the assumtion that it ends at "remaining length minus one" is false. For example if the root-dict of the torrent file contains a "signatures"-key (http://bittorrent.org/beps/bep_0035.html) – Encombe Jan 30 '15 at 08:58
  • The above code take such a dirty shortcut by not bdecode and find the exact end of the info dict, that I would classify it as faulty. See explanation why [**here**.](https://stackoverflow.com/questions/13670592/create-torrent-hash-info/19008851#19008851) A proper method how to calculate a infohash can be found [**here**.](https://stackoverflow.com/questions/19749085/calculating-the-info-hash-of-a-torrent-file/19800109#19800109) – Encombe Dec 01 '18 at 21:04
  • why `output.size() -1` ? It`s length – Hett Feb 17 '19 at 11:09
  • @Hett: It's answered in 1st paragraph of the answer. – BalusC Feb 18 '19 at 09:36
3

BitTorrent Specification

This should have everything you need, from a more official resource

Nicholas
  • 7,403
  • 10
  • 48
  • 76
  • As a complement to the official specification linked by Nicholas I would also recommend the inofficial specification wiki found at: https://wiki.theory.org/BitTorrentSpecification – Encombe Feb 04 '15 at 18:01
1

Can I calculate it using bencode?

No. That is for encoding bittorrent metadata, not actual files.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I've no idea. But that's a completely different question to the one you asked here. Why don't you focus on this one first ... and provide more information as requested above. – Stephen C Aug 09 '10 at 03:58