This commit is contained in:
2016-12-05 10:20:55 +01:00
parent 082c97fed9
commit a93c2ec7f8
3 changed files with 197 additions and 0 deletions

81
2016/05/Cargo.lock generated Normal file
View File

@@ -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"

7
2016/05/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "05"
version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies]
rust-crypto = "^0.2"

109
2016/05/src/main.rs Normal file
View File

@@ -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<String> {
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::<String>()
}
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<Option<char>> = 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::<String>()
}
#[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"));
}
}