dcrypt_api/traits/
serialize.rs

1// File: crates/api/src/traits/serialize.rs
2
3//! Traits for byte serialization of cryptographic types.
4
5use crate::Result;
6use zeroize::Zeroizing;
7
8/// A trait for public types that can be serialized to and from bytes.
9pub trait Serialize: Sized {
10    /// Creates an object from a byte slice.
11    fn from_bytes(bytes: &[u8]) -> Result<Self>;
12    /// Converts the object to a byte vector.
13    fn to_bytes(&self) -> Vec<u8>;
14}
15
16/// A trait for secret types that can be securely serialized.
17pub trait SerializeSecret: Sized {
18    /// Creates an object from a byte slice. Input should be zeroized after use.
19    fn from_bytes(bytes: &[u8]) -> Result<Self>;
20    /// Converts the object to a byte vector that is zeroized on drop.
21    fn to_bytes_zeroizing(&self) -> Zeroizing<Vec<u8>>;
22}