ps_datachunk/encrypted/
mod.rs1use std::sync::Arc;
2
3use crate::utils;
4use crate::DataChunk;
5use crate::Result;
6use crate::SerializedDataChunk;
7use ps_buffer::Buffer;
8use ps_cypher::Encrypted;
9use ps_hash::Hash;
10
11#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
12pub struct EncryptedDataChunk {
14 data: Buffer,
15 hash: Arc<Hash>,
16 key: Arc<Hash>,
17}
18
19impl EncryptedDataChunk {
20 pub fn decrypt(&self) -> Result<SerializedDataChunk> {
22 utils::decrypt(self.data_ref(), self.key.as_bytes())
23 }
24
25 #[must_use]
26 pub fn key(&self) -> Arc<Hash> {
27 self.key.clone()
28 }
29
30 #[must_use]
31 pub const fn key_ref(&self) -> &Arc<Hash> {
32 &self.key
33 }
34}
35
36impl DataChunk for EncryptedDataChunk {
37 fn data_ref(&self) -> &[u8] {
38 &self.data
39 }
40 fn hash_ref(&self) -> &Hash {
41 &self.hash
42 }
43 fn hash(&self) -> Arc<Hash> {
44 self.hash.clone()
45 }
46
47 fn into_owned(self) -> crate::OwnedDataChunk {
49 let Self { data, hash, key: _ } = self;
50
51 crate::OwnedDataChunk::from_data_and_hash(data, hash)
52 }
53}
54
55impl From<Encrypted> for EncryptedDataChunk {
56 fn from(value: Encrypted) -> Self {
57 Self {
58 data: value.bytes,
59 hash: value.hash,
60 key: value.key,
61 }
62 }
63}