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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/// Easy Hasher module
pub mod easy_hasher {
    type _Byte = u8;
    type _Data = Vec<_Byte>;
    type _Input<'a> = &'a String;


    pub struct Hash {
        hash: _Data
    }

    impl Hash {
        /// Performs the conversion from `Vec<u8>` to `String`
        pub fn hex_string(data: &_Data) -> String {
            data
                .iter()
                .map(|byte| format!("{:02x}", byte))
                .collect()
        }

        /// Express hash as hex string
        pub fn to_hex_string(&self) -> String {
            Hash::hex_string(&self.hash)
        }

        /// Get hash as `Vec<u8>`
        pub fn to_vec(&self) -> _Data {
            self.hash.clone()
        }
    }

    use sha3::Digest;
    use sha1::Sha1;

    /// Performs the conversion from `Vec<u8>` to `String`
    pub fn hex_string(data: _Data) -> String {
        data
            .iter()
            .map(|byte| format!("{:02x}", byte))
            .collect()
    }

    /// Generic SHA2/SHA3 hashing function
    fn sha3<T>(mut hasher: T, data: _Data) -> Hash where T: Digest {
        hasher.input(data.as_slice());
        Hash {
            hash: hasher
                .result()
                .to_vec()
        }
    }

    fn sha(data: _Data) -> Hash {
        let mut hasher = Sha1::new();
        hasher.update(data.as_slice());
        Hash {
            hash: hasher.digest().bytes().to_vec()
        }
    }
    /// CRC8 raw data hashing function, based on polynomial and initial value
    pub fn param_crc8(data: _Data, poly: u8, init: u8) -> Hash {
        let mut crc8 = crc8::Crc8::create_msb(poly);
        Hash {
            hash: vec![crc8.calc(data.as_slice(), data.len() as i32, init)]
        }
    }

    /* Raw data hashing functions */

    // CRC

    /// CRC8 raw data hashing function\
    /// poly = 0x07, init = 0x00
    pub fn crc8(d: _Data) -> Hash {
        param_crc8(d, 0x7, 0x0)
    }

    /// CRC16/ARC raw data hashing function
    pub fn crc16(d: _Data) -> Hash {
        Hash {
            hash: crc16::State::<crc16::ARC>::calculate(d.as_slice()).to_be_bytes().to_vec()
        }
    }

    /// CRC32 raw data hashing function
    pub fn crc32(d: _Data) -> Hash {
        Hash {
            hash: crc::crc32::checksum_ieee(d.as_slice()).to_be_bytes().to_vec()
        }
    }

    /// CRC64 raw data hashing function
    pub fn crc64(d: _Data) -> Hash {
        Hash {
            hash: crc::crc64::checksum_ecma(d.as_slice()).to_be_bytes().to_vec()
        }
    }

    // MD5

    /// MD5 raw data hashing function
    pub fn md5(d: _Data) -> Hash {
        Hash {
            hash: md5::compute(d).0.to_vec()
        }
    }

    // SHA1

    /// SHA1 raw data hashing function
    pub fn sha1(d: _Data) -> Hash {
        sha(d)
    }

    // SHA2

    /// SHA-224 raw data hashing function
    pub fn sha224(d: _Data) -> Hash {
        sha3(sha2::Sha224::new(), d)
    }

    /// SHA-256 raw data hashing function
    pub fn sha256(d: _Data) -> Hash {
        sha3(sha2::Sha256::new(), d)
    }

    /// SHA-384 raw data hashing function
    pub fn sha384(d: _Data) -> Hash {
        sha3(sha2::Sha384::new(), d)
    }

    /// SHA-512 raw data hashing function
    pub fn sha512(d: _Data) -> Hash {
        sha3(sha2::Sha512::new(), d)
    }

    // SHA3

    /// SHA3-224 raw data hashing function
    pub fn sha3_224(d: _Data) -> Hash {
        sha3(sha3::Sha3_224::new(), d)
    }

    /// SHA3-256 raw data hashing function
    pub fn sha3_256(d: _Data) -> Hash {
        sha3(sha3::Sha3_256::new(), d)
    }

    /// SHA3-384 raw data hashing function
    pub fn sha3_384(d: _Data) -> Hash {
        sha3(sha3::Sha3_384::new(), d)
    }

    /// SHA3-512 raw data hashing function
    pub fn sha3_512(d: _Data) -> Hash {
        sha3(sha3::Sha3_512::new(), d)
    }

    /* String hashing functions */

    // CRC

    /// CRC8 string hashing function\
    /// poly = 0x07, init = 0x00
    pub fn string_crc8(s: _Input) -> Hash {
        crc8(s.clone().into_bytes())
    }

    /// CRC16/ARC string hashing function
    pub fn string_crc16(s: _Input) -> Hash {
        crc16(s.clone().into_bytes())
    }

    /// CRC32 string hashing function
    pub fn string_crc32(s: _Input) -> Hash {
        crc32(s.clone().into_bytes())
    }

    /// CRC64 string hashing function
    pub fn string_crc64(s: _Input) -> Hash {
        crc64(s.clone().into_bytes())
    }

    // MD5

    /// MD5 string hashing function
    pub fn string_md5(s: _Input) -> Hash {
        md5(s.clone().into_bytes())
    }

    // SHA1

    /// SHA1 string hashing function
    pub fn string_sha1(s: _Input) -> Hash {
        sha1(s.clone().into_bytes())
    }

    // SHA2

    /// SHA-224 string hashing function
    pub fn string_sha224(s: _Input) -> Hash {
        sha224(s.clone().into_bytes())
    }

    /// SHA-256 string hashing function
    pub fn string_sha256(s: _Input) -> Hash {
        sha256(s.clone().into_bytes())
    }

    /// SHA-384 string hashing function
    pub fn string_sha384(s: _Input) -> Hash {
        sha384(s.clone().into_bytes())
    }

    /// SHA-512 string hashing function
    pub fn string_sha512(s: _Input) -> Hash {
        sha512(s.clone().into_bytes())
    }

    // SHA3

    /// SHA3-224 string hashing function
    pub fn string_sha3_224(s: _Input) -> Hash {
        sha3_224(s.clone().into_bytes())
    }

    /// SHA3-256 string hashing function
    pub fn string_sha3_256(s: _Input) -> Hash {
        sha3_256(s.clone().into_bytes())
    }

    /// SHA3-384 string hashing function
    pub fn string_sha3_384(s: _Input) -> Hash {
        sha3_384(s.clone().into_bytes())
    }

    /// SHA3-512 string hashing function
    pub fn string_sha3_512(s: _Input) -> Hash {
        sha3_512(s.clone().into_bytes())
    }
}