From bc79baf384f9055fe7c7ab719cffbd6ce152699b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Sun, 19 Mar 2023 23:08:40 +0100 Subject: =?UTF-8?q?za=C4=8Detki=20rusta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/.gitignore | 2 ++ rust/Cargo.toml | 14 ++++++++++++++ rust/src/main.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 rust/.gitignore create mode 100644 rust/Cargo.toml create mode 100644 rust/src/main.rs diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 0000000..1e7caa9 --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target/ diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..94d10fe --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "travnik" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bendy = "0.3.3" +chrono = "0.4.24" +hex = "0.4.3" +sha1 = "0.10.5" +sha2 = "0.10.6" +urlencoding = "2.1.2" diff --git a/rust/src/main.rs b/rust/src/main.rs new file mode 100644 index 0000000..64fa16c --- /dev/null +++ b/rust/src/main.rs @@ -0,0 +1,44 @@ +extern crate hex; +extern crate urlencoding; + +use std::collections::HashMap; +use std::net::IpAddr; +use chrono::{DateTime, Utc}; +use sha2::{Sha256, Digest}; +use sha1::{Sha1}; +enum Version { + V1, + V2, + Hybrid +} +struct Torrent { + name: String, + sha1: [u8; 20], + sha256: [u8; 32], + version: Version, + files: HashMap, + source: IpAddr, + date: DateTime, + hostname: String, + software: String +} +impl Torrent { + fn magnet(&self) -> String { + let mut string = String::from("magnet:?dn="); + string.push_str(&urlencoding::encode(&self.name)); + if matches!(self.version, Version::V1) || matches!(self.version, Version::Hybrid) { + string.push_str("&xt=urn:btih:"); + string.push_str(&hex::encode(&self.sha1)); + } + if matches!(self.version, Version::V2) || matches!(self.version, Version::Hybrid) { + string.push_str("&xt=urn:btmh:1220"); + string.push_str(&hex::encode(&self.sha256)); + } + string + } +} +fn main() { + let hash2 = Sha256::new().chain_update(b"test").finalize(); + let hash1 = Sha1::new().chain_update(b"test").finalize(); + println!("Hello, world! {:#?} {:#?}", hash2, hash1); +} -- cgit v1.2.3