Package com.frostwire.jlibtorrent
Class Sha256Hash
java.lang.Object
com.frostwire.jlibtorrent.Sha256Hash
- All Implemented Interfaces:
Cloneable,Comparable<Sha256Hash>
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 Summary
ConstructorsConstructorDescriptionConstructs an all-zero sha256-hashSha256Hash(byte[] bytes) Sha256Hash(com.frostwire.jlibtorrent.swig.sha256_hash h) Sha256Hash(String hex) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()set the sha256-hash to all zeroes.clone()intintbooleaninthashCode()booleanreturn true if the sha256-hash is all zero.static Sha256Hashmax()returns an all-F sha256-hash.static Sha256Hashmin()returns an all-zero sha256-hash.com.frostwire.jlibtorrent.swig.sha256_hashswig()toHex()Returns the hex representation of this has.toString()Returns an hex representation of this hash.
-
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
- 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
Returns the hex representation of this has.This method uses internally the libtorrent to_hex function.
- Returns:
- the hex representation
-
compareTo
- Specified by:
compareToin interfaceComparable<Sha256Hash>- Parameters:
o-- Returns:
-
toString
Returns an hex representation of this hash. Internally it calls . -
equals
-
hashCode
public int hashCode() -
clone
-
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
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()
-