digest_hash/lib.rs
1// Copyright 2017 Mikhail Zabaluev <mikhail.zabaluev@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Utilities for stable cryptographic hashing of data structures.
10//!
11//! # Example
12//!
13//! ```
14//! extern crate digest_hash;
15//! extern crate sha2;
16//!
17//! use digest_hash::{BigEndian, Hash};
18//! use digest_hash::EndianInput;
19//! use sha2::{Sha256, Digest};
20//!
21//! pub struct MyHashStableStruct {
22//! foo: u32,
23//! bar: i16
24//! }
25//!
26//! impl Hash for MyHashStableStruct {
27//! fn hash<H: EndianInput>(&self, digest: &mut H) {
28//! self.foo.hash(digest);
29//! self.bar.hash(digest);
30//! }
31//! }
32//!
33//! fn main() {
34//! let inst = MyHashStableStruct { foo: 0x01020304, bar: 0x0506 };
35//!
36//! let mut hasher = BigEndian::<Sha256>::new();
37//! inst.hash(&mut hasher);
38//! let hash = hasher.result();
39//!
40//! const EXPECTED: &[u8] =
41//! &[0x71, 0x92, 0x38, 0x5c, 0x3c, 0x06, 0x05, 0xde,
42//! 0x55, 0xbb, 0x94, 0x76, 0xce, 0x1d, 0x90, 0x74,
43//! 0x81, 0x90, 0xec, 0xb3, 0x2a, 0x8e, 0xed, 0x7f,
44//! 0x52, 0x07, 0xb3, 0x0c, 0xf6, 0xa1, 0xfe, 0x89];
45//! assert_eq!(hash.as_ref(), EXPECTED);
46//! }
47//! ```
48
49pub extern crate byteorder;
50pub extern crate digest;
51
52#[macro_use]
53mod macros;
54
55mod endian;
56mod hash;
57
58pub use endian::{BigEndian, LittleEndian, NetworkEndian};
59pub use endian::{Endian, EndianInput};
60pub use hash::Hash;
61
62#[path = "opinionated.rs"]
63pub mod personality;
64
65#[cfg(test)]
66mod testmocks;