dcrypt-api 4.0.0

Public API traits and types for the dcrypt library
Documentation
// File: crates/api/src/traits/serialize.rs

//! Traits for byte serialization of cryptographic types.

use crate::{Result, ZeroizingBytes};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::vec::Vec;

/// A trait for public types that can be serialized to and from bytes.
pub trait Serialize: Sized {
    /// Creates an object from a byte slice.
    fn from_bytes(bytes: &[u8]) -> Result<Self>;
    /// Converts the object to a byte vector.
    fn to_bytes(&self) -> Vec<u8>;
}

/// A trait for secret types that can be securely serialized.
pub trait SerializeSecret: Sized {
    /// Creates an object from a byte slice. Input should be zeroized after use.
    fn from_bytes(bytes: &[u8]) -> Result<Self>;
    /// Converts the object to exact-size bytes that are zeroized on drop.
    fn to_bytes_zeroizing(&self) -> ZeroizingBytes;
}