1 // Copyright 2023, The Android Open Source Project 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 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //! GBL Digest trait that defines interface for hash computation. 16 17 /// List of supported algorithms 18 #[derive(Debug, Eq, PartialEq, Clone, Copy)] 19 pub enum Algorithm { 20 /// SHA256 algorithm 21 SHA256, 22 /// SHA512 algorithm 23 SHA512, 24 } 25 26 /// Digest output trait that return algorithm and ref to the value 27 pub trait Digest: AsRef<[u8]> { 28 /// Get digest algorithm algorithm(&self) -> &Algorithm29 fn algorithm(&self) -> &Algorithm; 30 } 31 32 /// Context trait that implements digesting. 33 /// Sha256 or Sha512. 34 pub trait Context { 35 /// Digest type 36 type Digest: Digest; 37 38 /// Create [Context] object that can calculate digest with requested algorithm. 39 /// 40 /// # Arguments 41 /// 42 /// * algorithm - requested algorithm new(algorithm: Algorithm) -> Self43 fn new(algorithm: Algorithm) -> Self; 44 45 /// Process next portion of data for the digest. 46 /// 47 /// # Arguments 48 /// 49 /// * input - block of data to be processed update(&mut self, input: &[u8])50 fn update(&mut self, input: &[u8]); 51 52 /// Finalise digest computation. 53 /// 54 /// Object is consumed to prevent reusing. finish(self) -> Self::Digest55 fn finish(self) -> Self::Digest; 56 57 /// The algorithm that this context is using. algorithm(&self) -> &Algorithm58 fn algorithm(&self) -> &Algorithm; 59 } 60