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
//! An implementation of the [BLAKE2][1] hash functions.
//!
//! Based on the [blake2-rfc][2] crate.
//!
//! # Usage
//!
//! `Blake2b` can be used in the following way:
//!
//! ```rust
//! use blake2::{Blake2b, Digest};
//!
//! // create a Blake2b object
//! let mut hasher = Blake2b::new();
//!
//! // write input message
//! hasher.input(b"hello world");
//!
//! // read hash digest and consume hasher
//! let output = hasher.result();
//! println!("{:x}", output);
//! ```
//!
//! Same example for `Blake2s`:
//!
//! ```rust
//! use blake2::{Blake2s, Digest};
//!
//! let mut hasher = Blake2s::new();
//! hasher.input(b"hello world");
//! let output = hasher.result();
//! println!("{:x}", output);
//! ```
//!
//! ## Variable output size
//!
//! Both `Blake2b` and `Blake2s` support variable output sizes through
//! `VariableOutput` trait. `Input` trait has to be imported as well.
//!
//! ```rust
//! use blake2::Blake2b;
//! use blake2::digest::{Input, VariableOutput};
//!
//! let mut hasher = Blake2b::new(10).unwrap();
//! // instead of `input` method here we should use `process`
//! hasher.process(b"my_input");
//! let mut buf = [0u8; 10];
//! hasher.variable_result(&mut buf).unwrap();
//! assert_eq!(buf, [44, 197, 92, 132, 228, 22, 146, 78, 100, 0])
//! ```
//!
//! ## Message Authentication Code (MAC)
//!
//! BLAKE2 can be used as a MAC without any additionall constructs:
//!
//! ```rust
//! use blake2::Blake2b;
//! use blake2::crypto_mac::Mac;
//!
//! let mut hasher = Blake2b::new(b"my key").unwrap();
//! hasher.input(b"hello world");
//!
//! // `result` has type `MacResult` which is a thin wrapper around array of
//! // bytes for providing constant time equality check
//! let result = hasher.result();
//! // To get underlying array use `code` method, but be carefull, since
//! // incorrect use of the code value may permit timing attacks which defeat
//! // the security provided by the `MacResult`
//! let code_bytes = result.code();
//!
//! // To verify the message it's recommended to use `verify` method
//! let mut hasher = Blake2b::new(b"my key").unwrap();
//! hasher.input(b"hello world");
//! // `verify` return `Ok(())` if code is correct, `Err(MacError)` otherwise
//! hasher.verify(&code_bytes).unwrap();
//! ```
//!
//! [1]: https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2
//! [2]: https://github.com/cesarb/blake2-rfc
#![no_std]
#![warn(missing_docs)]

#![cfg_attr(feature = "simd", feature(platform_intrinsics, repr_simd))]
#![cfg_attr(feature = "simd_opt", feature(cfg_target_feature))]
#![cfg_attr(feature = "simd_asm", feature(asm))]

extern crate byte_tools;
#[macro_use]
pub extern crate digest;
pub extern crate crypto_mac;

mod consts;
mod as_bytes;

mod simd;


#[macro_use]
mod blake2;

mod blake2b;
mod blake2s;

pub use digest::Digest;
pub use blake2b::Blake2b;
pub use blake2s::Blake2s;