InfoHash

Enum InfoHash 

Source
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 string

Variants§

§

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).

Fields

§v1: InfoHashV1

The v1 (SHA1) info hash.

§v2: InfoHashV2

The v2 (SHA256) info hash.

Implementations§

Source§

impl InfoHash

Source

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.

Source

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.

Source

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.

Source

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());
Source

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).

Source

pub fn is_v1(&self) -> bool

Returns true if this is a v1 (SHA1) info hash.

Source

pub fn is_v2(&self) -> bool

Returns true if this is a v2 (SHA256) info hash.

Source

pub fn is_hybrid(&self) -> bool

Returns true if this is a hybrid info hash (BEP-47).

Source

pub fn v1_hash(&self) -> Option<InfoHashV1>

Returns the v1 hash if available.

Returns Some for V1 and Hybrid variants, None for V2.

Source

pub fn v2_hash(&self) -> Option<InfoHashV2>

Returns the v2 hash if available.

Returns Some for V2 and Hybrid variants, None for V1.

Source

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");
Source

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 Clone for InfoHash

Source§

fn clone(&self) -> InfoHash

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for InfoHash

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for InfoHash

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<InfoHashV1> for InfoHash

Source§

fn from(hash: InfoHashV1) -> Self

Converts to this type from the input type.
Source§

impl From<InfoHashV2> for InfoHash

Source§

fn from(hash: InfoHashV2) -> Self

Converts to this type from the input type.
Source§

impl Hash for InfoHash

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for InfoHash

Source§

fn eq(&self, other: &InfoHash) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for InfoHash

Source§

impl Eq for InfoHash

Source§

impl StructuralPartialEq for InfoHash

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more