1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//! Torrent identities. SHA-1 is used only as mandated by BitTorrent v1,
//! not as a choice for new authentication or security protocols.
use std::fmt;
use sha1::{Digest, Sha1};
/// The SHA-1 identity of a BitTorrent v1 info dictionary.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct InfoHashV1([u8; 20]);
impl InfoHashV1 {
/// Hash the exact original bencoded info bytes, never a re-encoded value.
///
/// This computes identity only; it does not validate the info dictionary.
pub fn from_info_bytes(original_info_bytes: &[u8]) -> Self {
Self(Sha1::digest(original_info_bytes).into())
}
/// Construct from an existing binary identity, e.g. a peer handshake.
pub const fn from_bytes(bytes: [u8; 20]) -> Self {
Self(bytes)
}
pub const fn as_bytes(&self) -> &[u8; 20] {
&self.0
}
}
impl fmt::Display for InfoHashV1 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.0 {
write!(f, "{byte:02x}")?;
}
Ok(())
}
}