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
//! Cryptographic hash functions primitives
//!
//! Via [Wikipedia](https://en.wikipedia.org/wiki/Cryptographic_hash_function):
//!
//! > The ideal cryptographic hash function has four main properties:
//! >
//! > - it is easy to compute the hash value for any given message
//! > - it is infeasible to generate a message from its hash
//! > - it is infeasible to modify a message without changing the hash
//! > - it is infeasible to find two different messages with the same hash.
//!
//! # Example
//!
//! Calculate SHA-512 sum:
//!
//! ```rust
//! # extern crate octavo_digest;
//! use octavo_digest::Digest;
//! use octavo_digest::sha2::Sha512;
//!
//! # fn main() {
//! # let data = "Hello World!";
//! let mut result = vec![0; Sha512::output_bytes()];
//! let mut sha = Sha512::default();
//!
//! sha.update(data);
//! sha.result(&mut result);
//!
//! for byte in result {
//!     print!("{:2x}", byte);
//! }
//! println!(" {}", data);
//! # }
//! ```

#![no_std]

#![forbid(overflowing_literals)]

extern crate generic_array;
extern crate static_buffer;
extern crate typenum;
extern crate byteorder;

use generic_array::ArrayLength;
use typenum::uint::Unsigned;

/// Hash function digest definition
pub trait Digest: Clone {
    /// Output size in bits
    type OutputBits: Unsigned + ArrayLength<u8>;
    /// Output size in bytes
    type OutputBytes: Unsigned + ArrayLength<u8>;

    /// Block size in bytes
    type BlockSize: Unsigned + ArrayLength<u8>;

    /// Update digest with data.
    fn update<T>(&mut self, input: T) where T: AsRef<[u8]>;

    /// Output size in bits
    fn output_bits() -> usize {
        Self::OutputBits::to_usize()
    }
    /// Output size in bytes
    fn output_bytes() -> usize {
        Self::OutputBytes::to_usize()
    }
    /// Block size in bytes
    fn block_size() -> usize {
        Self::BlockSize::to_usize()
    }

    /// Write resulting hash into `output`.
    ///
    /// `output` should be big enough to contain whole output.
    ///
    /// ## Panics
    ///
    /// If output length is less than `Digest::output_bytes`.
    fn result<T>(self, output: T) where T: AsMut<[u8]>;
}

/// Digest prelude
pub mod prelude {
    pub use Digest;

    pub use blake2;
    pub use md5::Md5;
    pub use ripemd::Ripemd160;
    pub use sha1::Sha1;
    pub use sha2;
    pub use sha3;
    pub use tiger;
}

pub mod blake2;
pub mod md5;
pub mod ripemd;
pub mod sha1;
pub mod sha2;
pub mod sha3;
pub mod tiger;
pub mod whirlpool;