Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit dcc64b3

Browse files
authored
Merge pull request #20 from dongcarl/2018-12-from-inner
[Discussion Needed] Allow constructing Hashes from underlying byte array
2 parents bba5e4d + 4bef1ba commit dcc64b3

File tree

8 files changed

+31
-0
lines changed

8 files changed

+31
-0
lines changed

src/hash160.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ impl HashTrait for Hash {
6767
fn into_inner(self) -> Self::Inner {
6868
self.0
6969
}
70+
71+
fn from_inner(inner: Self::Inner) -> Self {
72+
Hash(inner)
73+
}
7074
}
7175

7276
#[cfg(test)]

src/hmac.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ impl<T: Hash> Hash for Hmac<T> {
175175
fn into_inner(self) -> Self::Inner {
176176
self.0.into_inner()
177177
}
178+
179+
fn from_inner(inner: T::Inner) -> Self {
180+
Hmac(T::from_inner(inner))
181+
}
178182
}
179183

180184
#[cfg(feature="serde")]

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,7 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
119119

120120
/// Unwraps the hash and returns the underlying byte array
121121
fn into_inner(self) -> Self::Inner;
122+
123+
/// Constructs a hash from the underlying byte array
124+
fn from_inner(inner: Self::Inner) -> Self;
122125
}

src/ripemd160.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ impl HashTrait for Hash {
134134
fn into_inner(self) -> Self::Inner {
135135
self.0
136136
}
137+
138+
fn from_inner(inner: Self::Inner) -> Self {
139+
Hash(inner)
140+
}
137141
}
138142

139143
macro_rules! round(

src/sha1.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ impl HashTrait for Hash {
113113
fn into_inner(self) -> Self::Inner {
114114
self.0
115115
}
116+
117+
fn from_inner(inner: Self::Inner) -> Self {
118+
Hash(inner)
119+
}
116120
}
117121

118122
impl HashEngine {

src/sha256.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ impl HashTrait for Hash {
127127
fn into_inner(self) -> Self::Inner {
128128
self.0
129129
}
130+
131+
fn from_inner(inner: Self::Inner) -> Self {
132+
Hash(inner)
133+
}
130134
}
131135

132136
macro_rules! Ch( ($x:expr, $y:expr, $z:expr) => ($z ^ ($x & ($y ^ $z))) );

src/sha256d.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ impl HashTrait for Hash {
6363
fn into_inner(self) -> Self::Inner {
6464
self.0
6565
}
66+
67+
fn from_inner(inner: Self::Inner) -> Self {
68+
Hash(inner)
69+
}
6670
}
6771

6872
#[cfg(test)]

src/sha512.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ impl HashTrait for Hash {
184184
fn into_inner(self) -> Self::Inner {
185185
self.0
186186
}
187+
188+
fn from_inner(inner: Self::Inner) -> Self {
189+
Hash(inner)
190+
}
187191
}
188192

189193
macro_rules! Ch( ($x:expr, $y:expr, $z:expr) => ($z ^ ($x & ($y ^ $z))) );

0 commit comments

Comments
 (0)