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
macro_rules! circular_lshift32 (
($shift:expr, $w:expr) => (($w << $shift) | ($w >> (32 - $shift)))
);
macro_rules! circular_lshift64 (
($shift:expr, $w:expr) => (($w << $shift) | ($w >> (64 - $shift)))
);
macro_rules! hex_fmt_impl(
($imp:ident, $ty:ident) => (
impl ::std::fmt::$imp for $ty {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use hex::{format_hex, format_hex_reverse};
if $ty::DISPLAY_BACKWARD {
format_hex_reverse(&self.0, f)
} else {
format_hex(&self.0, f)
}
}
}
)
);
macro_rules! index_impl(
($ty:ty) => (
impl ::std::ops::Index<usize> for $ty {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
&self.0[index]
}
}
impl ::std::ops::Index<::std::ops::Range<usize>> for $ty {
type Output = [u8];
fn index(&self, index: ::std::ops::Range<usize>) -> &[u8] {
&self.0[index]
}
}
impl ::std::ops::Index<::std::ops::RangeFrom<usize>> for $ty {
type Output = [u8];
fn index(&self, index: ::std::ops::RangeFrom<usize>) -> &[u8] {
&self.0[index]
}
}
impl ::std::ops::Index<::std::ops::RangeTo<usize>> for $ty {
type Output = [u8];
fn index(&self, index: ::std::ops::RangeTo<usize>) -> &[u8] {
&self.0[index]
}
}
impl ::std::ops::Index<::std::ops::RangeFull> for $ty {
type Output = [u8];
fn index(&self, index: ::std::ops::RangeFull) -> &[u8] {
&self.0[index]
}
}
)
);
macro_rules! borrow_slice_impl(
($ty:ty, $inner:ty) => (
impl ::std::borrow::Borrow<[u8]> for $ty {
fn borrow(&self) -> &[u8] {
&self[..]
}
}
impl ::std::convert::AsRef<[u8]> for $ty {
fn as_ref(&self) -> &[u8] {
&self[..]
}
}
impl ::std::ops::Deref for $ty {
type Target = $inner;
fn deref(&self) -> &Self::Target {
&self.0
}
}
)
);
macro_rules! write_impl(
($ty:ty) => (
impl ::std::io::Write for $ty {
fn flush(&mut self) -> ::std::io::Result<()> {
Ok(())
}
#[cfg(not(feature = "fuzztarget"))]
fn write(&mut self, inp: &[u8]) -> ::std::io::Result<usize> {
let buf_idx = self.length % <Self as ::HashEngine>::BLOCK_SIZE;
let rem_len = <Self as ::HashEngine>::BLOCK_SIZE - buf_idx;
let write_len = ::std::cmp::min(rem_len, inp.len());
self.buffer[buf_idx..buf_idx + write_len].copy_from_slice(&inp[..write_len]);
self.length += write_len;
if self.length % <Self as ::HashEngine>::BLOCK_SIZE == 0 {
self.process_block();
}
Ok(write_len)
}
#[cfg(feature = "fuzztarget")]
fn write(&mut self, inp: &[u8]) -> ::std::io::Result<usize> {
for c in inp {
self.buffer[0] ^= *c;
}
self.length += inp.len();
Ok(inp.len())
}
}
)
);
#[cfg(test)]
mod test {
use Hash;
use sha256;
#[test]
fn borrow_slice_impl_to_vec() {
let hash = sha256::Hash::hash(&[3, 50]);
assert_eq!(hash.to_vec().len(), sha256::Hash::LEN);
}
}