crypto_hash/imp/
openssl.rs

1// Copyright (c) 2015, 2016 Mark Lee
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21//! A cryptographic hash digest generator dependent upon `OpenSSL`.
22
23#![warn(missing_docs)]
24
25use super::Algorithm;
26use openssl::hash;
27use std::io;
28
29/// Generator of digests using a cryptographic hash function.
30///
31/// # Examples
32///
33/// ```rust
34/// use crypto_hash::{Algorithm, Hasher};
35/// use std::io::Write;
36///
37/// let mut hasher = Hasher::new(Algorithm::SHA256);
38/// hasher.write_all(b"crypto");
39/// hasher.write_all(b"-");
40/// hasher.write_all(b"hash");
41/// let result = hasher.finish();
42/// let expected =
43///     b"\xfd\x1a\xfb`\"\xcdMG\xc8\x90\x96\x1cS9(\xea\xcf\xe8!\x9f\x1b%$\xf7\xfb*a\x84}\xdf\x8c'"
44///     .to_vec();
45/// assert_eq!(expected, result)
46/// ```
47pub struct Hasher(hash::Hasher);
48
49impl Hasher {
50    /// Create a new `Hasher` for the given `Algorithm`.
51    pub fn new(algorithm: Algorithm) -> Hasher {
52        let hash_type = match algorithm {
53            Algorithm::MD5 => hash::MessageDigest::md5(),
54            Algorithm::SHA1 => hash::MessageDigest::sha1(),
55            Algorithm::SHA256 => hash::MessageDigest::sha256(),
56            Algorithm::SHA512 => hash::MessageDigest::sha512(),
57        };
58
59        match hash::Hasher::new(hash_type) {
60            Ok(hasher) => Hasher(hasher),
61            Err(error_stack) => panic!("OpenSSL error(s): {}", error_stack),
62        }
63    }
64
65    /// Generate a digest from the data written to the `Hasher`.
66    pub fn finish(&mut self) -> Vec<u8> {
67        let Hasher(ref mut hasher) = *self;
68        match hasher.finish() {
69            Ok(digest) => digest.to_vec(),
70            Err(error_stack) => panic!("OpenSSL error(s): {}", error_stack),
71        }
72    }
73}
74
75impl io::Write for Hasher {
76    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
77        let Hasher(ref mut hasher) = *self;
78        hasher.write(buf)
79    }
80
81    fn flush(&mut self) -> io::Result<()> {
82        let Hasher(ref mut hasher) = *self;
83        hasher.flush()
84    }
85}