Unverified Commit c9765ed8 authored by Hero Bird's avatar Hero Bird Committed by GitHub

Slight improvements to hash module - take 2 (#376)

* [core] add Debug impls to hash abstractions

* [core] add Write impl for std builds of hash::Wrap
parent 46b28234
Pipeline #86716 passed with stages
in 11 minutes and 54 seconds
...@@ -12,6 +12,11 @@ ...@@ -12,6 +12,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#[cfg(feature = "std")]
use ::std::io::{
Result as IoResult,
Write,
};
use ink_prelude::vec::Vec; use ink_prelude::vec::Vec;
/// Hash builder that accumulates a buffer on the contract side. /// Hash builder that accumulates a buffer on the contract side.
...@@ -74,6 +79,18 @@ pub struct Wrap<'a> { ...@@ -74,6 +79,18 @@ pub struct Wrap<'a> {
len: usize, len: usize,
} }
#[cfg(feature = "std")]
impl<'a> Write for Wrap<'a> {
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
<Self as Accumulator>::write(self, buf);
Ok(buf.len())
}
fn flush(&mut self) -> IoResult<()> {
Ok(())
}
}
impl<'a> From<&'a mut [u8]> for Wrap<'a> { impl<'a> From<&'a mut [u8]> for Wrap<'a> {
fn from(buffer: &'a mut [u8]) -> Self { fn from(buffer: &'a mut [u8]) -> Self {
Self { buffer, len: 0 } Self { buffer, len: 0 }
......
...@@ -24,6 +24,7 @@ use core::marker::PhantomData; ...@@ -24,6 +24,7 @@ use core::marker::PhantomData;
/// ///
/// This means that a hash builder with this type as accumulator cannot /// This means that a hash builder with this type as accumulator cannot
/// build hashes for instances based on their SCALE encoding. /// build hashes for instances based on their SCALE encoding.
#[derive(Debug)]
pub enum NoAccumulator {} pub enum NoAccumulator {}
/// Generic hash builder to construct hashes given a builder strategy. /// Generic hash builder to construct hashes given a builder strategy.
...@@ -42,6 +43,7 @@ pub enum NoAccumulator {} ...@@ -42,6 +43,7 @@ pub enum NoAccumulator {}
/// - [`Keccak256`](`crate::hash::Keccak256`) /// - [`Keccak256`](`crate::hash::Keccak256`)
/// - [`Blake2x256`](`crate::hash::Blake2x256`) /// - [`Blake2x256`](`crate::hash::Blake2x256`)
/// - [`Blake2x128`](`crate::hash::Blake2x128`) /// - [`Blake2x128`](`crate::hash::Blake2x128`)
#[derive(Debug)]
pub struct HashBuilder<H, S = NoAccumulator> { pub struct HashBuilder<H, S = NoAccumulator> {
/// The strategy used to build up the hash. /// The strategy used to build up the hash.
strategy: S, strategy: S,
......
...@@ -47,17 +47,21 @@ macro_rules! impl_hasher_for { ...@@ -47,17 +47,21 @@ macro_rules! impl_hasher_for {
} }
impl_hasher_for! { impl_hasher_for! {
/// SHA2 256-bit hasher. /// SHA2 256-bit hasher.
#[derive(Debug)]
struct Sha2x256Hasher(sha2_256, 32); struct Sha2x256Hasher(sha2_256, 32);
} }
impl_hasher_for! { impl_hasher_for! {
/// KECCAK 256-bit hasher. /// KECCAK 256-bit hasher.
#[derive(Debug)]
struct Keccak256Hasher(keccak_256, 32); struct Keccak256Hasher(keccak_256, 32);
} }
impl_hasher_for! { impl_hasher_for! {
/// BLAKE2 256-bit hasher. /// BLAKE2 256-bit hasher.
#[derive(Debug)]
struct Blake2x256Hasher(blake2_256, 32); struct Blake2x256Hasher(blake2_256, 32);
} }
impl_hasher_for! { impl_hasher_for! {
/// BLAKE2 128-bit hasher. /// BLAKE2 128-bit hasher.
#[derive(Debug)]
struct Blake2x128Hasher(blake2_128, 16); struct Blake2x128Hasher(blake2_128, 16);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment