Class InfoHash

java.lang.Object
com.frostwire.jlibtorrent.InfoHash

public class InfoHash extends Object
Dual-hash support for BitTorrent v1 (SHA-1) and v2 (SHA-256) torrents.

InfoHash holds the cryptographic hash(es) that uniquely identify a torrent's content. Modern torrents support both v1 hashes (BEP 3, SHA-1, 20 bytes) and v2 hashes (BEP 52, SHA-256, 32 bytes) to enable backward compatibility with older clients while providing cryptographic advantages of SHA-256. A torrent can have v1 only, v2 only, or both v1 and v2 hashes simultaneously.

Understanding Torrent Hashes:

  • v1 Hash (SHA-1): 20-byte hash (160 bits), used by original BitTorrent protocol (BEP 3)
  • v2 Hash (SHA-256): 32-byte hash (256 bits), improved security and file verification (BEP 52)
  • Dual Mode: Modern torrents include both hashes for maximum compatibility and security
  • Hash Truncation: If only v1 is available, it may be a truncated v2 hash for DHT discovery

Hash Types and Semantics:

 Pure v1 Torrent:
   hasV1() = true, hasV2() = false
   Contains only SHA-1 hash (20 bytes)
   Compatible with older BitTorrent clients
   Less secure than v2

 Pure v2 Torrent:
   hasV1() = false, hasV2() = true
   Contains only SHA-256 hash (32 bytes)
   Only works with BitTorrent v2 capable clients
   Maximum security and file integrity checking

 Hybrid v1+v2 Torrent:
   hasV1() = true, hasV2() = true
   Contains both SHA-1 and SHA-256 hashes
   Best practice: works everywhere, maximum compatibility
   Both hashes are independent and complete
 

Accessing Torrent Hashes:

 // From torrent metadata
 TorrentInfo torrentInfo = new TorrentInfo(torrentFile);
 InfoHash infoHash = torrentInfo.infoHash();  // Gets the InfoHash wrapper

 // Check which hashes are available
 if (infoHash.hasV1()) {
     Sha1Hash v1 = infoHash.getV1();  // Get SHA-1 hash (20 bytes)
     System.out.println("v1 hash: " + v1.toHex());
 }

 if (infoHash.hasV2()) {
     Sha256Hash v2 = infoHash.getV2();  // Get SHA-256 hash (32 bytes)
     System.out.println("v2 hash: " + v2.toHex());
 }

 // Get the "best" hash (prefers v2 for security)
 Sha1Hash best = infoHash.getBest();  // Returns v2 if available, else v1
 System.out.println("Best hash: " + best.toHex());
 

Hash Selection Strategy:

 // The getBest() method uses this logic:
 // 1. If v2 hash exists, return it (SHA-256, more secure)
 // 2. Otherwise return v1 hash (SHA-1, backward compatibility)
 // 3. Never returns null - at least one hash always exists

 // Use v1 for compatibility with older peers
 Sha1Hash v1 = infoHash.getV1();
 sm.find(v1);  // Look up torrent by v1 hash

 // Use v2 for security-critical operations
 Sha256Hash v2 = infoHash.getV2();
 // DHT lookups, peer discovery, verification
 

Torrent Lookup by Hash:

 // Find torrent in SessionManager by v1 hash
 Sha1Hash v1 = infoHash.getV1();
 TorrentHandle handle = sm.find(v1);

 // Find torrent by v2 hash
 Sha256Hash v2 = infoHash.getV2();
 TorrentHandle handle = sm.find(v2);

 // Get best available hash for reliable lookup
 Sha1Hash bestHash = infoHash.getBest();  // Returns available hash
 TorrentHandle handle = sm.find(bestHash);  // Works with either v1 or v2
 

BitTorrent v1 vs v2 Comparison:

BitTorrent v1 vs v2 Hash Comparison
Featurev1 (SHA-1)v2 (SHA-256)
Hash Size20 bytes32 bytes
AlgorithmSHA-1SHA-256
SecurityDeprecatedSecure
CompatibilityUniversalv2+ clients only
BEPBEP 3 (original)BEP 52 (modern)

Performance Notes:

  • Hash lookups are O(1) - direct field access
  • Use the specific getV1() or getV2() if you know which hash you need
  • getBest() prefers v2 but returns v1 as fallback - safe for all cases
  • Hybrid torrents use the same hash for discovery via DHT regardless of peer version
See Also:
  • Constructor Details

    • InfoHash

      public InfoHash(com.frostwire.jlibtorrent.swig.info_hash_t swig)
    • InfoHash

      public InfoHash()
  • Method Details

    • swig

      public com.frostwire.jlibtorrent.swig.info_hash_t swig()
    • hasV1

      public boolean hasV1()
    • hasV2

      public boolean hasV2()
    • getBest

      public Sha1Hash getBest()
    • getV1

      public Sha1Hash getV1()
    • getV2

      public Sha256Hash getV2()