1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use crate::DataChunkTrait;
use crate::OwnedDataChunk;
use crate::PsDataChunkError;
use ps_cypher::Compressor;
use ps_hash::Hash;

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
/// represents an encrypted chunk of data and the key needed to decrypt it
pub struct EncryptedDataChunk {
    pub chunk: OwnedDataChunk,
    pub key: std::sync::Arc<Hash>,
}

impl EncryptedDataChunk {
    /// Decrypts this `EncryptedDataChunk`.
    pub fn decrypt(&self, compressor: &Compressor) -> Result<OwnedDataChunk, PsDataChunkError> {
        OwnedDataChunk::decrypt(&self.chunk, self.key.as_bytes(), compressor)
    }
}

impl DataChunkTrait for EncryptedDataChunk {
    fn data_ref(&self) -> &[u8] {
        self.chunk.data_ref()
    }
    fn hash_ref(&self) -> &[u8] {
        self.chunk.hash_ref()
    }
    fn hash(&self) -> crate::HashCow {
        self.chunk.hash().into()
    }
}