From a93c2ec7f8b34f931f2067a51614a82f7d4e2d63 Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Mon, 5 Dec 2016 10:20:55 +0100 Subject: [PATCH] 2016/05 --- 2016/05/Cargo.lock | 81 ++++++++++++++++++++++++++++++++ 2016/05/Cargo.toml | 7 +++ 2016/05/src/main.rs | 109 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 2016/05/Cargo.lock create mode 100644 2016/05/Cargo.toml create mode 100644 2016/05/src/main.rs diff --git a/2016/05/Cargo.lock b/2016/05/Cargo.lock new file mode 100644 index 0000000..c7945b1 --- /dev/null +++ b/2016/05/Cargo.lock @@ -0,0 +1,81 @@ +[root] +name = "05" +version = "0.1.0" +dependencies = [ + "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gcc" +version = "0.3.39" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rust-crypto" +version = "0.2.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.39 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "time" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum gcc 0.3.39 (registry+https://github.com/rust-lang/crates.io-index)" = "771e4a97ff6f237cf0f7d5f5102f6e28bb9743814b6198d684da5c58b76c11e0" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70" +"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" +"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" +"checksum rustc-serialize 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)" = "bff9fc1c79f2dec76b253273d07682e94a978bd8f132ded071188122b2af9818" +"checksum time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "3c7ec6d62a20df54e07ab3b78b9a3932972f4b7981de295563686849eb3989af" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/2016/05/Cargo.toml b/2016/05/Cargo.toml new file mode 100644 index 0000000..846cf84 --- /dev/null +++ b/2016/05/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "05" +version = "0.1.0" +authors = ["logaritmisk "] + +[dependencies] +rust-crypto = "^0.2" diff --git a/2016/05/src/main.rs b/2016/05/src/main.rs new file mode 100644 index 0000000..2421665 --- /dev/null +++ b/2016/05/src/main.rs @@ -0,0 +1,109 @@ +extern crate crypto; + +use std::u64; + +use crypto::md5::Md5; +use crypto::digest::Digest; + + +struct Hasher { + md5: Md5, + index: u64, + secret: String +} + +impl Hasher { + pub fn new(secret: &str) -> Self { + Hasher { + md5: Md5::new(), + index: 0, + secret: String::from(secret) + } + } +} + +impl Iterator for Hasher { + type Item = String; + + fn next(&mut self) -> Option { + let input = format!("{}{}", self.secret, self.index); + + self.md5.reset(); + self.md5.input(input.as_bytes()); + + self.index += 1; + + Some(self.md5.result_str()) + } +} + + +fn main() { + let password = generate_week_password("ojvtpuvg"); + + println!("week_password={}", password); + + let password = generate_strong_password("ojvtpuvg"); + + println!("strong_password={}", password); +} + +pub fn generate_week_password(sector_id: &str) -> String { + Hasher::new(sector_id) + .filter(|x| x.starts_with("00000")) + .map(|x| x.as_bytes()[5] as char) + .take(8) + .collect::() +} + +pub fn generate_strong_password(sector_id: &str) -> String { + let iter = Hasher::new(sector_id) + .filter(|x| x.starts_with("00000")) + .filter_map(|x| { + let bytes = x.as_bytes(); + + if (bytes[5] as char) < '8' { + Some(((bytes[5] - 48) as usize, bytes[6] as char)) + } else { + None + } + }); + + let mut password: Vec> = vec![None; 8]; + + let mut i = 0; + + for (pos, ch) in iter { + if password[pos].is_some() { + continue; + } + + password[pos] = Some(ch); + + i += 1; + + if i >= 8 { + break; + } + } + + password.iter() + .filter_map(|x| *x) + .collect::() +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn example_01() { + assert_eq!(String::from("18f47a30"), generate_week_password("abc")); + } + + #[test] + fn example_02() { + assert_eq!(String::from("05ace8e3"), generate_strong_password("abc")); + } +}