瀏覽代碼

Base functionality working.

themultiplexer 8 年之前
父節點
當前提交
2e76a936a1
共有 2 個文件被更改,包括 44 次插入29 次删除
  1. 18
    7
      hw7/task1/src/hash256.rs
  2. 26
    22
      hw7/task1/src/main.rs

+ 18
- 7
hw7/task1/src/hash256.rs 查看文件

@@ -1,13 +1,24 @@
1
-use hasher_sha256;
1
+use hasher_sha256::{Hasher, HashResult};
2
+use hasher_sha256::Sha256;
2 3
 
3
-struct Solution {
4
-    number: usize,
5
-    hash: String,
4
+
5
+pub struct Solution {
6
+    pub number: usize,
7
+    pub hash: String,
6 8
 }
7 9
 
8
-pub fn verify_product(base: usize, number: usize, difficulty: String) -> Option<Solution> {
9
-    if let x = hasher_sha256::Hasher::hash(((base * number) as i32)).hex().ends_with(difficulty) {
10
-        Some(Solution{ number: (base * number), hash: x,});
10
+pub fn verify_product(base: usize, number: usize, difficulty: &String) -> Option<Solution> {
11
+    let sol = base * number;
12
+    let input = sol.to_string();
13
+    let bytes = input.as_bytes();
14
+
15
+    let hash = Sha256::hash(bytes).hex();
16
+
17
+    //println!("{} ends with {}", hash, difficulty);
18
+
19
+    if hash.ends_with(difficulty) {
20
+        return Some(Solution{ number: number, hash: hash,});
11 21
     }
22
+
12 23
     None
13 24
 }

+ 26
- 22
hw7/task1/src/main.rs 查看文件

@@ -1,20 +1,35 @@
1 1
 extern crate clap;
2 2
 extern crate time;
3 3
 use clap::{Arg, App, SubCommand};
4
+use std::process;
4 5
 mod hasher_sha256;
5 6
 mod hash256;
6 7
 
7
-pub fn main(/*base: usize, */number: usize, diff: String) {
8
+pub fn main() {
8 9
     let matches = create_app().get_matches();
9
-    let base;
10
-    match matches.value_of("base") {
11
-        Some(x) => {base = u64::from(x)}
12
-        None => {panic!()}
13
-    };
14
-    match hash256::verify_product(base, number, diff) {
15
-        Some(x) => { println!("{}", diff); }
16
-        None => { println!("None"); }
10
+    let base = matches.value_of("base").unwrap_or("1");
11
+    let diff = matches.value_of("difficulty").unwrap_or("1");
12
+
13
+    println!("Using base: {}", base);
14
+    println!("Using difficulty: {}", diff);
15
+
16
+    if diff.chars().any( |c| !c.is_digit(16)) {
17
+        println!("Difficulty is not hexadecimal.");
18
+        process::exit(1)
17 19
     }
20
+
21
+    match base.parse::<usize>() {
22
+        Ok(b) => {
23
+            println!("Please wait...");
24
+            for n in 0..<usize>::max_value() {
25
+                if let Some(x) = hash256::verify_product(b, n, &diff.to_string()) {
26
+                    println!("Number: {} --> hash: {}", x.number, x.hash);
27
+                    process::exit(0)
28
+                }
29
+            }
30
+        }
31
+        Err(_) => {}
32
+    };
18 33
 }
19 34
 
20 35
 fn create_app<'a, 'b>() -> App<'a, 'b> {
@@ -23,23 +38,12 @@ fn create_app<'a, 'b>() -> App<'a, 'b> {
23 38
         .author("Lorenz Bung & Joshua Rutschmann")
24 39
         .about("Calculates the Hashvalue of the given base, number and difficulty.")
25 40
         .arg(Arg::with_name("base")
26
-            .short("b")
27
-            .long("base")
28
-            .value_name("VALUE")
41
+            .value_name("base")
29 42
             .help("The base of the hash to be calculated on.")
30 43
             .takes_value(true)
31 44
             .required(true))
32
-        .arg(Arg::with_name("number")
33
-            .short("n")
34
-            .long("number")
35
-            .value_name("VALUE")
36
-            .help("The number of the hash to be calculated on.")
37
-            .takes_value(true)
38
-            .required(true))
39 45
         .arg(Arg::with_name("difficulty")
40
-            .short("d")
41
-            .long("difficulty")
42
-            .value_name("VALUE")
46
+            .value_name("difficulty")
43 47
             .help("The difficulty of the calculated hash.")
44 48
             .takes_value(true)
45 49
             .required(true))

Loading…
取消
儲存