hashes: Move from_engine functions

In order to use the `general_hash_type` macro the must exist a
standalone `from_engine` function. Currently this function is in
different places in different modules. In an effort to make the `hashes`
code easier to reason about put the functions right below the macro.

Code move only, no other changes.
This commit is contained in:
Tobin C. Harding 2025-01-30 10:42:10 +11:00
parent 98db7bca74
commit b98c489066
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 18 additions and 18 deletions

View File

@ -15,6 +15,15 @@ crate::internal_macros::general_hash_type! {
"Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))"
}
fn from_engine(e: HashEngine) -> Hash {
let sha2 = sha256::Hash::from_engine(e.0);
let rmd = ripemd160::Hash::hash(sha2.as_byte_array());
let mut ret = [0; 20];
ret.copy_from_slice(rmd.as_byte_array());
Hash(ret)
}
/// Engine to compute HASH160 hash function.
#[derive(Clone)]
pub struct HashEngine(sha256::HashEngine);
@ -34,15 +43,6 @@ impl crate::HashEngine for HashEngine {
fn n_bytes_hashed(&self) -> u64 { self.0.n_bytes_hashed() }
}
fn from_engine(e: HashEngine) -> Hash {
let sha2 = sha256::Hash::from_engine(e.0);
let rmd = ripemd160::Hash::hash(sha2.as_byte_array());
let mut ret = [0; 20];
ret.copy_from_slice(rmd.as_byte_array());
Hash(ret)
}
#[cfg(test)]
mod tests {
#[test]

View File

@ -10,6 +10,15 @@ crate::internal_macros::general_hash_type! {
"Output of the SHA256d hash function."
}
fn from_engine(e: HashEngine) -> Hash {
let sha2 = sha256::Hash::from_engine(e.0);
let sha2d = sha256::Hash::hash(sha2.as_byte_array());
let mut ret = [0; 32];
ret.copy_from_slice(sha2d.as_byte_array());
Hash(ret)
}
/// Engine to compute SHA256d hash function.
#[derive(Clone)]
pub struct HashEngine(sha256::HashEngine);
@ -29,15 +38,6 @@ impl crate::HashEngine for HashEngine {
fn n_bytes_hashed(&self) -> u64 { 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.as_byte_array());
let mut ret = [0; 32];
ret.copy_from_slice(sha2d.as_byte_array());
Hash(ret)
}
#[cfg(test)]
mod tests {
#[allow(unused_imports)] // whether this is used depends on features