Class Sha256Hash

java.lang.Object
com.frostwire.jlibtorrent.Sha256Hash
All Implemented Interfaces:
Cloneable, Comparable<Sha256Hash>

public final class Sha256Hash extends Object implements Comparable<Sha256Hash>, Cloneable
Immutable 32-byte SHA-256 hash wrapper for BitTorrent v2 torrents.

Sha256Hash represents a 256-bit SHA-256 cryptographic hash. It's primarily used in BitTorrent v2 (BEP 52) which introduced stronger cryptography than the original SHA-1 based v1.

BitTorrent v1 vs v2 vs Hybrid:

  • v1 torrents: Use SHA-1 info-hash (20 bytes, )
  • v2 torrents: Use SHA-256 info-hash (32 bytes, Sha256Hash)
  • Hybrid torrents: Have BOTH SHA-1 and SHA-256 info-hashes for backward compatibility

Creating Sha256Hash Objects:

 // From hex string (64 characters for SHA-256)
 Sha256Hash hash = new Sha256Hash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

 // From 32-byte array
 byte[] bytes = new byte[32];
 Sha256Hash hash = new Sha256Hash(bytes);

 // All-zeros hash
 Sha256Hash zeroHash = new Sha256Hash();

 // Minimum/maximum values
 Sha256Hash min = Sha256Hash.min();  // All zeros
 Sha256Hash max = Sha256Hash.max();  // All 0xFF
 

Using Sha256Hash for v2 Torrent Lookups:

 TorrentInfo ti = new TorrentInfo(torrentFile);
 Sha256Hash infoHashV2 = ti.infoHashV2();

 // Find the v2 torrent in the session
 TorrentHandle th = sessionManager.find(infoHashV2);
 if (th != null) {
     System.out.println("v2 Torrent found: " + th.status().name());
 }
 

Hybrid Torrent Handling:

 TorrentInfo ti = new TorrentInfo(torrentFile);

 // Hybrid torrents have both hashes
 Sha1Hash v1Hash = ti.infoHashV1();
 Sha256Hash v2Hash = ti.infoHashV2();

 // The session will use the best available hash
 TorrentHandle th = sessionManager.find(v2Hash);
 if (th == null) {
     // Fallback to v1 if v2 not found
     th = sessionManager.find(v1Hash);
 }
 

Hash Operations:

 Sha256Hash hash = new Sha256Hash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

 // Convert to string formats
 String hex = hash.toHex();           // 64-character hex string
 String toString = hash.toString();   // Also hex

 // Properties
 boolean isZero = hash.isAllZeros();
 int leadingZeroBits = hash.countLeadingZeroes();

 // Comparison and equality
 Sha256Hash hash2 = new Sha256Hash("...");
 int cmp = hash.compareTo(hash2);
 boolean equal = hash.equals(hash2);
 int code = hash.hashCode();

 // Cloning
 Sha256Hash copy = hash.clone();
 

Note: For legacy BitTorrent v1 torrents, use (20 bytes). The class will automatically provide the appropriate hash type.

See Also:
  • Constructor Details

    • Sha256Hash

      public Sha256Hash(com.frostwire.jlibtorrent.swig.sha256_hash h)
      Parameters:
      h - native object
    • Sha256Hash

      public Sha256Hash()
      Constructs an all-zero sha256-hash
    • Sha256Hash

      public Sha256Hash(byte[] bytes)
      Parameters:
      bytes - hash as an array of bytes
    • Sha256Hash

      public Sha256Hash(String hex)
      Parameters:
      hex - hex coded representation of the hash
  • Method Details

    • clear

      public void clear()
      set the sha256-hash to all zeroes.
    • isAllZeros

      public boolean isAllZeros()
      return true if the sha256-hash is all zero.
      Returns:
      true if zero
    • countLeadingZeroes

      public int countLeadingZeroes()
      Returns:
      the number of leading zeroes
    • toHex

      public String toHex()
      Returns the hex representation of this has.

      This method uses internally the libtorrent to_hex function.

      Returns:
      the hex representation
    • compareTo

      public int compareTo(Sha256Hash o)
      Specified by:
      compareTo in interface Comparable<Sha256Hash>
      Parameters:
      o -
      Returns:
    • toString

      public String toString()
      Returns an hex representation of this hash. Internally it calls .
      Overrides:
      toString in class Object
      Returns:
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
      Parameters:
      obj -
      Returns:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
      Returns:
    • clone

      public Sha256Hash clone()
      Overrides:
      clone in class Object
    • max

      public static Sha256Hash max()
      returns an all-F sha256-hash. i.e. the maximum value representable by a 256 bit number (32 bytes). This is a static member function.
      Returns:
      the maximum number
    • min

      public static Sha256Hash min()
      returns an all-zero sha256-hash. i.e. the minimum value representable by a 256 bit number (32 bytes). This is a static member function.
      Returns:
      the minimum number (zero)
    • swig

      public com.frostwire.jlibtorrent.swig.sha256_hash swig()