arrayref.rs (1707B)
1 // Copyright 2023 Cryspen Sarl 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 9 //! This module contains the traits and related errors for hashers that take array references as 10 //! arguments and write the outputs to mutable array references. 11 //! 12 13 #[derive(Debug, PartialEq)] 14 /// Error indicating that hashing failed. 15 pub enum HashError { 16 /// The length of the provided payload is invalid. 17 InvalidPayloadLength, 18 } 19 20 impl core::fmt::Display for HashError { 21 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 22 let text = match self { 23 HashError::InvalidPayloadLength => "the length of the provided payload is invalid", 24 }; 25 26 f.write_str(text) 27 } 28 } 29 30 #[cfg(feature = "error-in-core")] 31 mod error_in_core { 32 33 impl core::error::Error for super::HashError {} 34 } 35 36 /// A trait for incremental hashing, where the output is written into a provided buffer. 37 pub trait DigestIncremental<const OUTPUT_LEN: usize>: super::DigestIncrementalBase { 38 /// Writes the digest into `digest`. 39 /// 40 /// Note that the digest state can be continued to be used, to extend the digest. 41 fn finish(state: &mut Self::IncrementalState, digest: &mut [u8; OUTPUT_LEN]); 42 } 43 44 /// A trait for oneshot hashing, where the output is written into a provided buffer. 45 pub trait Hash<const OUTPUT_LEN: usize> { 46 /// Writes the digest for the given input byte slice, into `digest` in immediate mode. 47 fn hash(digest: &mut [u8; OUTPUT_LEN], payload: &[u8]) -> Result<(), HashError>; 48 }