2015/04
This commit is contained in:
48
2015/04/src/main.rs
Normal file
48
2015/04/src/main.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
extern crate crypto;
|
||||
|
||||
|
||||
use std::u64;
|
||||
|
||||
use crypto::md5::Md5;
|
||||
use crypto::digest::Digest;
|
||||
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", process("ckczppom", "000000"));
|
||||
}
|
||||
|
||||
fn process(secret: &str, starts_with: &str) -> Option<u64> {
|
||||
let mut md5 = Md5::new();
|
||||
|
||||
for i in 1..u64::MAX {
|
||||
let input = format!("{}{}", secret, i);
|
||||
|
||||
md5.input(input.as_bytes());
|
||||
|
||||
let result = md5.result_str();
|
||||
|
||||
if result.starts_with(starts_with) {
|
||||
return Some(i);
|
||||
}
|
||||
|
||||
md5.reset();
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::process;
|
||||
|
||||
#[test]
|
||||
fn example_01() {
|
||||
assert_eq!(Some(609043), process("abcdef", "00000"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_02() {
|
||||
assert_eq!(Some(1048970), process("pqrstuv", "00000"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user