tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

owned.rs (1671B)


      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 return values as arrays.
     11 //!
     12 
     13 use super::arrayref;
     14 
     15 pub use arrayref::HashError;
     16 
     17 /// A trait for oneshot hashing, where the output is returned as an array.
     18 pub trait Hash<const OUTPUT_LEN: usize> {
     19    /// Returns the digest for the given input byte slice, as an array, in immediate mode.
     20    fn hash(payload: &[u8]) -> Result<[u8; OUTPUT_LEN], HashError>;
     21 }
     22 
     23 /// A trait for incremental hashing, where the output is returned as an array.
     24 pub trait DigestIncremental<const OUTPUT_LEN: usize>: super::DigestIncrementalBase {
     25    /// Returns the digest as an array.
     26    ///
     27    /// Note that the digest state can be continued to be used, to extend the digest.
     28    fn finish(state: &mut Self::IncrementalState) -> [u8; OUTPUT_LEN];
     29 }
     30 
     31 impl<const OUTPUT_LEN: usize, D: arrayref::Hash<OUTPUT_LEN>> Hash<OUTPUT_LEN> for D {
     32    fn hash(payload: &[u8]) -> Result<[u8; OUTPUT_LEN], HashError> {
     33        let mut digest = [0; OUTPUT_LEN];
     34        Self::hash(&mut digest, payload).map(|_| digest)
     35    }
     36 }
     37 impl<const OUTPUT_LEN: usize, D: arrayref::DigestIncremental<OUTPUT_LEN>>
     38    DigestIncremental<OUTPUT_LEN> for D
     39 {
     40    fn finish(state: &mut Self::IncrementalState) -> [u8; OUTPUT_LEN] {
     41        let mut digest = [0; OUTPUT_LEN];
     42        Self::finish(state, &mut digest);
     43        digest
     44    }
     45 }