1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Provides utilities for interaction with hashing algorithms

extern crate crypto;

use crypto::digest::Digest;

///Represents checksum algorithm
pub struct Checksum {
    name: String,
    algo: Box<Digest>
}

impl Checksum {
    ///Constructs new ```Checksum```
    pub fn new<T: 'static + Digest>(algo_name: String, algorithm: T) -> Checksum {
        Checksum {
            name: algo_name,
            algo: Box::new(algorithm)
        }
    }

    #[inline]
    ///Provides input for algorithm
    pub fn input(&mut self, slice_content: &[u8]) {
        self.algo.input(slice_content);
    }

    #[inline]
    ///Returns result in the following format: ```{name} - {hash}```
    pub fn result(&mut self) -> String {
        format!("{:8} - {}", self.name, self.algo.result_str())
    }

    #[inline(always)]
    ///Returns hashsum
    pub fn checksum(&mut self) -> String {
        self.algo.result_str()
    }

    #[inline(always)]
    ///Resets hashing
    pub fn reset(&mut self) {
        self.algo.reset();
    }

    #[inline]
    ///Returns algorithm's name for file extension(lowercase)
    pub fn get_file_ext(&self) -> String {
        self.name[0..self.name.len()-1].chars()
                                       .map(|elem| elem.to_lowercase().next().unwrap())
                                       .collect()
    }

    #[inline(always)]
    ///Returns name of algorithm in format ```{name} -```
    pub fn get_type_string(&self) -> String {
        format!("{:8} - ", self.name)
    }
}

impl PartialEq for Checksum {
    fn eq(&self, right: &Checksum) -> bool {
        self.name == right.name
    }

    fn ne(&self, right: &Checksum) -> bool {
        self.name != right.name
    }
}