Class Sha1Hash

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

public final class Sha1Hash extends Object implements Comparable<Sha1Hash>, Cloneable
Immutable 20-byte SHA-1 hash wrapper.

Sha1Hash represents a 160-bit SHA-1 cryptographic hash. In the bittorrent world, it's used to uniquely identify content and torrents:

  • Info-hash V1: SHA-1 of the torrent's info dictionary (uniquely identifies the torrent)
  • Piece hashes: SHA-1 of each 16KB-16MB block of data (verifies downloaded content)
  • Peer IDs: 20-byte identifier for each peer client
  • DHT Node IDs: 20-byte identifiers for DHT network nodes

Creating Sha1Hash Objects:

 // From hex string (40 characters)
 Sha1Hash hash = new Sha1Hash("d8e8fca2dc0f896fd7cb4cb0031ba249");

 // From 20-byte array
 byte[] bytes = new byte[20];
 Sha1Hash hash = new Sha1Hash(bytes);

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

Using Sha1Hash for Torrent Lookups:

 TorrentInfo ti = new TorrentInfo(torrentFile);
 Sha1Hash infoHash = ti.infoHashV1();

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

Hash Operations:

 Sha1Hash hash = new Sha1Hash("d8e8fca2dc0f896fd7cb4cb0031ba249");

 // Convert to string formats
 String hex = hash.toHex();           // Hex representation
 String hex_lower = hash.toHexLower(); // Lowercase hex

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

 // Comparison
 Sha1Hash hash2 = new Sha1Hash("...");
 int cmp = hash.compareTo(hash2);
 boolean equal = hash.equals(hash2);
 

Note: For BitTorrent v2 torrents, use instead. Hybrid torrents (v1 + v2) will have both SHA-1 and SHA-256 info-hashes.

See Also:
  • Constructor Details

    • Sha1Hash

      public Sha1Hash(com.frostwire.jlibtorrent.swig.sha1_hash h)
      Parameters:
      h - native object
    • Sha1Hash

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

      public Sha1Hash(String hex)
      Parameters:
      hex - hex coded representation of the hash
    • Sha1Hash

      public Sha1Hash()
      Constructs an all-zero sha1-hash
  • Method Details

    • swig

      public com.frostwire.jlibtorrent.swig.sha1_hash swig()
      Returns:
      the native object
    • clear

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

      public boolean isAllZeros()
      return true if the sha1-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(Sha1Hash o)
      Specified by:
      compareTo in interface Comparable<Sha1Hash>
      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 Sha1Hash clone()
      Overrides:
      clone in class Object
    • max

      public static Sha1Hash max()
      returns an all-F sha1-hash. i.e. the maximum value representable by a 160 bit number (20 bytes). This is a static member function.
      Returns:
      the maximum number
    • min

      public static Sha1Hash min()
      returns an all-zero sha1-hash. i.e. the minimum value representable by a 160 bit number (20 bytes). This is a static member function.
      Returns:
      the minimum number (zero)