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
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![deny(missing_docs)]
#![allow(bare_trait_objects)]
#![allow(ellipsis_inclusive_range_patterns)]
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
#[cfg(all(test, feature = "unstable"))] extern crate test;
#[cfg(any(test, feature="std"))] extern crate core;
#[cfg(feature="core2")] extern crate core2;
#[cfg(feature = "alloc")] extern crate alloc;
#[cfg(all(not(feature = "alloc"), feature = "std"))] use std as alloc;
#[cfg(feature="serde")] pub extern crate serde;
#[cfg(all(test,feature="serde"))] extern crate serde_test;
#[doc(hidden)]
pub mod _export {
pub mod _core {
pub use ::core::*;
}
}
#[cfg(feature = "schemars")] extern crate schemars;
#[macro_use] mod util;
#[macro_use] pub mod serde_macros;
#[cfg(any(feature = "std", feature = "core2"))] mod impls;
pub mod error;
pub mod hex;
pub mod hash160;
pub mod hmac;
pub mod ripemd160;
pub mod sha1;
pub mod sha256;
pub mod sha256d;
pub mod sha256t;
pub mod siphash24;
pub mod sha512;
pub mod cmp;
use core::{borrow, fmt, hash, ops};
pub use hmac::{Hmac, HmacEngine};
pub use error::Error;
pub trait HashEngine: Clone + Default {
type MidState;
fn midstate(&self) -> Self::MidState;
const BLOCK_SIZE: usize;
fn input(&mut self, data: &[u8]);
fn n_bytes_hashed(&self) -> usize;
}
pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex +
ops::Index<ops::RangeFull, Output = [u8]> +
ops::Index<ops::RangeFrom<usize>, Output = [u8]> +
ops::Index<ops::RangeTo<usize>, Output = [u8]> +
ops::Index<ops::Range<usize>, Output = [u8]> +
ops::Index<usize, Output = u8> +
borrow::Borrow<[u8]>
{
type Engine: HashEngine;
type Inner: hex::FromHex;
fn engine() -> Self::Engine {
Self::Engine::default()
}
fn from_engine(e: Self::Engine) -> Self;
const LEN: usize;
fn from_slice(sl: &[u8]) -> Result<Self, Error>;
fn hash(data: &[u8]) -> Self {
let mut engine = Self::engine();
engine.input(data);
Self::from_engine(engine)
}
const DISPLAY_BACKWARD: bool = false;
fn into_inner(self) -> Self::Inner;
fn as_inner(&self) -> &Self::Inner;
fn from_inner(inner: Self::Inner) -> Self;
}
#[cfg(test)]
mod tests {
use Hash;
hash_newtype!(TestNewtype, ::sha256d::Hash, 32, doc="A test newtype");
hash_newtype!(TestNewtype2, ::sha256d::Hash, 32, doc="A test newtype");
#[test]
fn convert_newtypes() {
let h1 = TestNewtype::hash(&[]);
let h2: TestNewtype2 = h1.as_hash().into();
assert_eq!(&h1[..], &h2[..]);
let h = ::sha256d::Hash::hash(&[]);
let h2: TestNewtype = h.to_string().parse().unwrap();
assert_eq!(h2.as_hash(), h);
}
}