// Copyright 2023 Cryspen Sarl // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 //! This module contains the traits and related errors for hashers that take array references as //! arguments and return values as arrays. //! use super::arrayref; pub use arrayref::HashError; /// A trait for oneshot hashing, where the output is returned as an array. pub trait Hash { /// Returns the digest for the given input byte slice, as an array, in immediate mode. fn hash(payload: &[u8]) -> Result<[u8; OUTPUT_LEN], HashError>; } /// A trait for incremental hashing, where the output is returned as an array. pub trait DigestIncremental: super::DigestIncrementalBase { /// Returns the digest as an array. /// /// Note that the digest state can be continued to be used, to extend the digest. fn finish(state: &mut Self::IncrementalState) -> [u8; OUTPUT_LEN]; } impl> Hash for D { fn hash(payload: &[u8]) -> Result<[u8; OUTPUT_LEN], HashError> { let mut digest = [0; OUTPUT_LEN]; Self::hash(&mut digest, payload).map(|_| digest) } } impl> DigestIncremental for D { fn finish(state: &mut Self::IncrementalState) -> [u8; OUTPUT_LEN] { let mut digest = [0; OUTPUT_LEN]; Self::finish(state, &mut digest); digest } }