hashes: Strongly type the sha256d::HashEngine
Currently we are using a type alias for the `sha256d::HashEngine`. Type alias' allow for potential mixing of types, a `sha256d::HashEngine` struct can better serve our users with not much additional complexity or maintenance burden.
This commit is contained in:
parent
a7422a779c
commit
d5dd54a489
|
@ -59,12 +59,12 @@ impl_message_from_hash!(SegwitV0Sighash);
|
||||||
// but outside of it, it should not be possible to construct these hash
|
// but outside of it, it should not be possible to construct these hash
|
||||||
// types from arbitrary data (except by casting via to/from_byte_array).
|
// types from arbitrary data (except by casting via to/from_byte_array).
|
||||||
impl LegacySighash {
|
impl LegacySighash {
|
||||||
fn engine() -> sha256::HashEngine { sha256d::Hash::engine() }
|
fn engine() -> sha256d::HashEngine { sha256d::Hash::engine() }
|
||||||
fn from_engine(e: sha256::HashEngine) -> Self { Self(sha256d::Hash::from_engine(e)) }
|
fn from_engine(e: sha256d::HashEngine) -> Self { Self(sha256d::Hash::from_engine(e)) }
|
||||||
}
|
}
|
||||||
impl SegwitV0Sighash {
|
impl SegwitV0Sighash {
|
||||||
fn engine() -> sha256::HashEngine { sha256d::Hash::engine() }
|
fn engine() -> sha256d::HashEngine { sha256d::Hash::engine() }
|
||||||
fn from_engine(e: sha256::HashEngine) -> Self { Self(sha256d::Hash::from_engine(e)) }
|
fn from_engine(e: sha256d::HashEngine) -> Self { Self(sha256d::Hash::from_engine(e)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
sha256t_hash_newtype! {
|
sha256t_hash_newtype! {
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
use bitcoin_io::impl_write;
|
use bitcoin_io::impl_write;
|
||||||
|
|
||||||
use crate::{hmac, ripemd160, sha1, sha256, sha512, siphash24, HashEngine};
|
use crate::{hmac, ripemd160, sha1, sha256, sha256d, sha512, siphash24, HashEngine};
|
||||||
|
|
||||||
impl_write!(
|
impl_write!(
|
||||||
sha1::HashEngine,
|
sha1::HashEngine,
|
||||||
|
@ -26,6 +26,15 @@ impl_write!(
|
||||||
|_us| { Ok(()) }
|
|_us| { Ok(()) }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
impl_write!(
|
||||||
|
sha256d::HashEngine,
|
||||||
|
|us: &mut sha256d::HashEngine, buf| {
|
||||||
|
us.input(buf);
|
||||||
|
Ok(buf.len())
|
||||||
|
},
|
||||||
|
|_us| { Ok(()) }
|
||||||
|
);
|
||||||
|
|
||||||
impl_write!(
|
impl_write!(
|
||||||
sha512::HashEngine,
|
sha512::HashEngine,
|
||||||
|us: &mut sha512::HashEngine, buf| {
|
|us: &mut sha512::HashEngine, buf| {
|
||||||
|
|
|
@ -13,10 +13,30 @@ crate::internal_macros::hash_type! {
|
||||||
"Output of the SHA256d hash function."
|
"Output of the SHA256d hash function."
|
||||||
}
|
}
|
||||||
|
|
||||||
type HashEngine = sha256::HashEngine;
|
/// Engine to compute SHA256d hash function.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct HashEngine(sha256::HashEngine);
|
||||||
|
|
||||||
fn from_engine(e: sha256::HashEngine) -> Hash {
|
impl HashEngine {
|
||||||
let sha2 = sha256::Hash::from_engine(e);
|
/// Creates a new SHA256d hash engine.
|
||||||
|
pub const fn new() -> Self { Self(sha256::HashEngine::new()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for HashEngine {
|
||||||
|
fn default() -> Self { Self::new() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl crate::HashEngine for HashEngine {
|
||||||
|
type MidState = sha256::Midstate;
|
||||||
|
fn midstate(&self) -> Self::MidState { self.0.midstate() }
|
||||||
|
|
||||||
|
const BLOCK_SIZE: usize = 64; // Same as sha256::HashEngine::BLOCK_SIZE;
|
||||||
|
fn input(&mut self, data: &[u8]) { self.0.input(data) }
|
||||||
|
fn n_bytes_hashed(&self) -> usize { self.0.n_bytes_hashed() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_engine(e: HashEngine) -> Hash {
|
||||||
|
let sha2 = sha256::Hash::from_engine(e.0);
|
||||||
let sha2d = sha256::Hash::hash(&sha2[..]);
|
let sha2d = sha256::Hash::hash(&sha2[..]);
|
||||||
|
|
||||||
let mut ret = [0; 32];
|
let mut ret = [0; 32];
|
||||||
|
|
Loading…
Reference in New Issue