deps: localy provided bencode implmentation#366
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #366 +/- ##
===========================================
- Coverage 85.02% 83.95% -1.08%
===========================================
Files 89 102 +13
Lines 6565 7267 +702
===========================================
+ Hits 5582 6101 +519
- Misses 983 1166 +183
... and 2 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
50573fc to
fc10b0a
Compare
|
Hi @da2ce7, I want to fix this bug. I need to calculate the infohash from the torrent file. We are using serde_bencode, but it does not work to calculate the infohash because we cannot include the custom fields. I would like to use a different bencoding crate to calculate the infohash: Something like this: use bendy::decoding::{Error as BencodeError, FromBencode, Object};
use sha1::{Digest, Sha1};
use std::fs;
use std::path::Path;
fn calculate_infohash<P: AsRef<Path>>(path: P) -> Result<[u8; 20], BencodeError> {
// Read and decode the torrent file
let data = fs::read(path)?;
let torrent = Object::decode(&data)?;
// Find the "info" dictionary
let info = torrent
.lookup("info")
.ok_or(BencodeError::Unspecified)?
.to_owned();
// Encode the "info" dictionary into bytes
let info_bytes = info.encode()?;
// Calculate the SHA-1 hash
let mut hasher = Sha1::new();
hasher.update(&info_bytes);
let result = hasher.finalize();
// Convert the hash to a 20-byte array and return
Ok(result.into())
}
fn main() {
let path = "path_to_torrent_file.torrent";
match calculate_infohash(path) {
Ok(hash) => {
println!("Infohash: {:?}", hash);
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}I need to use a bencoding crate:
Since we are already using use bip_bencode::{Bencode, BencodeRef, BencodeConvert};
use sha1::{Digest, Sha1};
use std::fs;
use std::io::{self, Read};
use std::path::Path;
fn calculate_infohash<P: AsRef<Path>>(path: P) -> io::Result<[u8; 20]> {
// Read the torrent file
let mut data = Vec::new();
let mut file = fs::File::open(path)?;
file.read_to_end(&mut data)?;
// Decode the torrent file
let torrent = BencodeRef::decode(&data).unwrap();
// Extract the "info" dictionary
let info = match torrent.dict().unwrap().lookup(b"info") {
Some(info) => info,
None => return Err(io::Error::new(io::ErrorKind::Other, "Missing info dictionary")),
};
// Re-encode the "info" dictionary into bytes
let info_bytes = info.encode();
// Calculate the SHA-1 hash
let mut hasher = Sha1::new();
hasher.update(&info_bytes);
let result = hasher.finalize();
Ok(result.into())
}
fn main() {
let path = "path_to_torrent_file.torrent";
match calculate_infohash(path) {
Ok(hash) => {
println!("Infohash: {:?}", hash);
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}I do not like to parse the torrent bytes twice (with serde_bencode to extract the structs and with bip_encode to extract the info dict and calculate the infohash), but I want to fix the bug asap. After fixing the bug, we can consider using only the bip_encode and construct structs manually instead of using serde_bencode. |
fc10b0a to
a4ac682
Compare
|
ACK a4ac682 |
No description provided.