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#[cfg(all(not(feature = "std"), feature = "alloc"))]
9use alloc::vec::Vec;
10#[cfg(feature = "std")]
11use std::vec::Vec;
12
13/// A trait for public types that can be serialized to and from bytes.
14pub trait Serialize: Sized {
15 /// Creates an object from a byte slice.
16 fn from_bytes(bytes: &[u8]) -> Result<Self>;
17 /// Converts the object to a byte vector.
18 fn to_bytes(&self) -> Vec<u8>;
19}
20
21/// A trait for secret types that can be securely serialized.
22pub trait SerializeSecret: Sized {
23 /// Creates an object from a byte slice. Input should be zeroized after use.
24 fn from_bytes(bytes: &[u8]) -> Result<Self>;
25 /// Converts the object to a byte vector that is zeroized on drop.
26 fn to_bytes_zeroizing(&self) -> Zeroizing<Vec<u8>>;
27}