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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/// Easy Hasher module
pub mod easy_hasher {
    type _Byte = u8;
    type _Data = Vec<_Byte>;
    type _Input<'a> = &'a String;
    use sha3::Digest;
    use sha1::Sha1;

    mod filedata {
        use std::fs;
        use std::io::Read;
        use crate::easy_hasher::_Data;

        pub(crate) struct FileData {
            fp: fs::File,
            fm: fs::Metadata,
        }

        impl FileData {
            pub fn open<P: AsRef<std::path::Path>>(p: P) -> Result<FileData, String> {
                let _fp = fs::File::open(p);
                let _file: fs::File;
                match _fp {
                    Ok(f) => _file = f,
                    Err(e) => return Err(e.to_string())
                };

                let _fm = _file.metadata();
                let _meta: fs::Metadata;
                match _fm {
                    Ok(m) => _meta = m,
                    Err(e) => return Err(e.to_string())
                }

                Ok(
                    FileData {
                        fp: _file,
                        fm: _meta,
                    }
                )
            }

            /// Get file contents as byte vector
            pub fn to_vec(mut self) -> Result<_Data, String> {
                let mut vec = vec![0; self.fm.len() as usize];
                let _res = self.fp.read(&mut vec);
                match _res {
                    Ok(r) => if vec.len() == r {
                        Ok(vec)
                    } else {
                        Err("error reading from file".to_string())
                    },
                    Err(e) => Err(e.to_string()),
                }
            }

            /// Get file object
            #[allow(dead_code)]
            pub fn file(self) -> fs::File {
                self.fp
            }

            /// Get file metadata object
            #[allow(dead_code)]
            pub fn metadata(self) -> fs::Metadata {
                self.fm
            }
        }
    }

    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()
        }
    }

    /// 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)]
        }
    }

    /// Generic-algorithm file hasher
    pub fn file_hash(hasher: fn(_Data) -> Hash, filename: _Input) -> Result<Hash, String> {
        use filedata::*;
        let rfd = FileData::open(filename);
        let fd: FileData;

        match rfd {
            Ok(data) => {
                fd = data
            },
            Err(e) => {
                return Err(e)
            },
        }

        let rcont = fd.to_vec();
        let cont: _Data;

        match rcont {
            Ok(bytes) => cont = bytes,
            Err(e) => return Err(e),
        }

        Ok(hasher(cont))
    }

    /* file hashing */

    /// CRC8 file hashing function\
    /// poly = 0x07, init = 0x00
    pub fn file_crc8(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_crc8, filename)
    }

    /// CRC16/ARC file hashing function
    pub fn file_crc16(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_crc16, filename)
    }

    /// CRC32/IEEE file hashing function
    pub fn file_crc32(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_crc32, filename)
    }

    /// CRC64/ECMA file hashing function
    pub fn file_crc64(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_crc64, filename)
    }

    /// MD5 file hashing function
    pub fn file_md5(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_md5, filename)
    }

    /// SHA-1 file hashing function
    pub fn file_sha1(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_sha1, filename)
    }

    /// SHA-224 file hashing function
    pub fn file_sha224(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_sha224, filename)
    }

    /// SHA-256 file hashing function
    pub fn file_sha256(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_sha256, filename)
    }

    /// SHA-384 file hashing function
    pub fn file_sha384(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_sha384, filename)
    }

    /// SHA-512 file hashing function
    pub fn file_sha512(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_sha512, filename)
    }

    /// SHA3-224 file hashing function
    pub fn file_sha3_224(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_sha3_224, filename)
    }

    /// SHA3-256 file hashing function
    pub fn file_sha3_256(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_sha3_256, filename)
    }

    /// SHA3-384 file hashing function
    pub fn file_sha3_384(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_sha3_384, filename)
    }

    /// SHA3-512 file hashing function
    pub fn file_sha3_512(filename: _Input) -> Result<Hash, String> {
        file_hash(raw_sha3_512, filename)
    }

    /* Raw data hashing functions */

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

    /// CRC16/ARC raw data hashing function
    pub fn raw_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 raw_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 raw_crc64(d: _Data) -> Hash {
        Hash {
            hash: crc::crc64::checksum_ecma(d.as_slice()).to_be_bytes().to_vec()
        }
    }

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

    /// SHA-1 raw data hashing function
    pub fn raw_sha1(d: _Data) -> Hash {
        sha(d)
    }

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

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

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

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

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

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

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

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

    /* String hashing functions */

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

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

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

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

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

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

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

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

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

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

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

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

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

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