intermodal/src/md5_digest.rs
Casey Rodarmor 6549850dac
Add initial implementation of imdl torrent verify
Adds the command `imdl` torrent verify` to verify the contents of torrents.

This implementation is extremely naive. It does successfully verify torrents,
but it will produce unsatisfying results when a torrent fails verification. In
particular, it won't give any information about which pieces in a file were
corrupt.

type: added
2020-04-07 19:01:05 -07:00

54 lines
1.1 KiB
Rust

use crate::common::*;
#[serde(transparent)]
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Copy, Clone)]
pub(crate) struct Md5Digest {
#[serde(with = "SerHex::<serde_hex::Strict>")]
bytes: [u8; 16],
}
impl Md5Digest {
#[cfg(test)]
pub(crate) fn from_hex(hex: &str) -> Md5Digest {
assert_eq!(hex.len(), 32);
let mut bytes: [u8; 16] = [0; 16];
for n in 0..16 {
let i = n * 2;
bytes[n] = u8::from_str_radix(&hex[i..i + 2], 16).unwrap();
}
Md5Digest { bytes }
}
}
impl From<md5::Digest> for Md5Digest {
fn from(digest: md5::Digest) -> Self {
Md5Digest { bytes: digest.0 }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ser() {
let digest = Md5Digest {
bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
};
let bytes = bendy::serde::ser::to_bytes(&digest).unwrap();
assert_eq!(
str::from_utf8(&bytes).unwrap(),
"32:000102030405060708090a0b0c0d0e0f"
);
let string_bytes = bendy::serde::ser::to_bytes(&"000102030405060708090a0b0c0d0e0f").unwrap();
assert_eq!(bytes, string_bytes);
}
}