use typebit::links::{parse_link, ContentFamily, ContentId, DownloadLink};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let links = [
"magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567&dn=ubuntu.iso",
"ed2k://|file|hello.bin|3|a448017aaf21d8525fc10ae87aa6729d|/",
"thunder://QUFodHRwOi8vZXhhbXBsZS5jb20vZi5yYXJaWg==",
"qqdl://aHR0cDovL2V4YW1wbGUuY29tL2YucmFy",
"flashget://[FLASHGET]QUFodHRwOi8vZXhhbXBsZS5jb20vZi5yYXJaWg==[/FLASHGET]",
"ipfs://QmNp5n7FFav5ZDaHAj6HzuhJ8LDbL1N6NRzAgT6piWS2Kx",
"kad://00112233445566778899aabbccddeeff@127.0.0.1:4661",
"https://cdn.example.com/data.bin",
"https://pan.baidu.com/s/1abcDEF123?pwd=uvw4",
"https://pan.xunlei.com/s/AbCdEfGh?pwd=1234",
];
for raw in links {
match parse_link(raw) {
Ok(link) => println!("[{:>10}] {raw}", link.kind()),
Err(e) => println!("[{:>10}] {raw} -> error: {e:?}", "!!"),
}
}
println!();
let data = b"the quick brown fox jumps over the lazy dog";
let id = ContentId::digest_of(ContentFamily::Sha256, data);
println!("sha256(id) = {}", hex(id.digest()));
println!("verify(original) = {}", id.verify(data));
println!("verify(tampered) = {}", id.verify(b"tampered"));
println!("receipt root (32 B) = {}", hex(&id.to_root()));
match parse_link(links[1]).unwrap() {
DownloadLink::Ed2k(f) => {
println!(
"\ned2k file: {} ({} bytes), MD4 = {}",
f.name,
f.size,
hex(&f.hash)
);
}
other => println!("unexpected: {other:?}"),
}
Ok(())
}
fn hex(b: &[u8]) -> String {
let mut s = String::with_capacity(b.len() * 2);
for x in b {
s.push_str(&format!("{x:02x}"));
}
s
}