pub enum InfoHash {
V1([u8; 20]),
V2([u8; 32]),
Hybrid {
v1: InfoHashV1,
v2: InfoHashV2,
},
}Expand description
A BitTorrent info hash identifying a torrent.
The info hash is a cryptographic hash of the torrent’s info dictionary,
used to uniquely identify a torrent across the BitTorrent network.
§Versions
- V1: 20-byte SHA1 hash (original BitTorrent, BEP-3)
- V2: 32-byte SHA256 hash (BitTorrent v2, BEP-52)
- Hybrid: Both v1 and v2 hashes (BEP-47 hybrid torrents)
§Examples
use rbit::metainfo::InfoHash;
// Parse from a hex string (automatically detects version)
let v1_hash = InfoHash::from_hex("c12fe1c06bba254a9dc9f519b335aa7c1367a88a").unwrap();
assert!(v1_hash.is_v1());
assert_eq!(v1_hash.as_bytes().len(), 20);
// Create from raw bytes
let bytes = [0u8; 20];
let hash = InfoHash::from_v1_bytes(&bytes).unwrap();
// Display as hex
println!("{}", hash); // prints 40-character hex stringVariants§
V1([u8; 20])
BitTorrent v1 info hash (20-byte SHA1).
V2([u8; 32])
BitTorrent v2 info hash (32-byte SHA256).
Hybrid
Hybrid torrent with both v1 and v2 info hashes (BEP-47).
Implementations§
Source§impl InfoHash
impl InfoHash
Sourcepub fn from_v1_bytes(bytes: &[u8]) -> Result<Self, MetainfoError>
pub fn from_v1_bytes(bytes: &[u8]) -> Result<Self, MetainfoError>
Creates a v1 info hash from a 20-byte slice.
§Errors
Returns MetainfoError::InvalidInfoHashLength if the slice is not exactly 20 bytes.
Sourcepub fn from_v2_bytes(bytes: &[u8]) -> Result<Self, MetainfoError>
pub fn from_v2_bytes(bytes: &[u8]) -> Result<Self, MetainfoError>
Creates a v2 info hash from a 32-byte slice.
§Errors
Returns MetainfoError::InvalidInfoHashLength if the slice is not exactly 32 bytes.
Sourcepub fn hybrid(v1: InfoHashV1, v2: InfoHashV2) -> Self
pub fn hybrid(v1: InfoHashV1, v2: InfoHashV2) -> Self
Creates a hybrid info hash with both v1 and v2 hashes.
This is used for BEP-47 hybrid torrents that support both v1 and v2 protocols.
Sourcepub fn from_hex(s: &str) -> Result<Self, MetainfoError>
pub fn from_hex(s: &str) -> Result<Self, MetainfoError>
Parses an info hash from a hexadecimal string.
The version is determined by the string length:
- 40 characters → v1 (20 bytes)
- 64 characters → v2 (32 bytes)
§Errors
Returns MetainfoError::InvalidInfoHashLength if the string length
is invalid or contains non-hex characters.
§Examples
use rbit::metainfo::InfoHash;
let hash = InfoHash::from_hex("c12fe1c06bba254a9dc9f519b335aa7c1367a88a").unwrap();
assert!(hash.is_v1());Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns the raw bytes of the info hash.
For v1, returns 20 bytes. For v2, returns 32 bytes. For hybrid, returns the v1 hash bytes (20 bytes).
Sourcepub fn v1_hash(&self) -> Option<InfoHashV1>
pub fn v1_hash(&self) -> Option<InfoHashV1>
Returns the v1 hash if available.
Returns Some for V1 and Hybrid variants, None for V2.
Sourcepub fn v2_hash(&self) -> Option<InfoHashV2>
pub fn v2_hash(&self) -> Option<InfoHashV2>
Returns the v2 hash if available.
Returns Some for V2 and Hybrid variants, None for V1.
Sourcepub fn to_hex(&self) -> String
pub fn to_hex(&self) -> String
Converts the info hash to a lowercase hexadecimal string.
For hybrid hashes, returns the v1 hash hex string.
§Examples
use rbit::metainfo::InfoHash;
let hash = InfoHash::from_v1_bytes(&[0xab; 20]).unwrap();
assert_eq!(hash.to_hex(), "abababababababababababababababababababab");Sourcepub fn url_encode(&self) -> String
pub fn url_encode(&self) -> String
URL-encodes the info hash for use in tracker announce requests.
For hybrid hashes, encodes the v1 hash.
Trait Implementations§
Source§impl From<InfoHashV1> for InfoHash
impl From<InfoHashV1> for InfoHash
Source§fn from(hash: InfoHashV1) -> Self
fn from(hash: InfoHashV1) -> Self
Source§impl From<InfoHashV2> for InfoHash
impl From<InfoHashV2> for InfoHash
Source§fn from(hash: InfoHashV2) -> Self
fn from(hash: InfoHashV2) -> Self
impl Copy for InfoHash
impl Eq for InfoHash
impl StructuralPartialEq for InfoHash
Auto Trait Implementations§
impl Freeze for InfoHash
impl RefUnwindSafe for InfoHash
impl Send for InfoHash
impl Sync for InfoHash
impl Unpin for InfoHash
impl UnwindSafe for InfoHash
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.