trelent-hyok 0.1.12

A Rust library implementing Hold Your Own Key (HYOK) encryption patterns with support for multiple cloud providers
Documentation
//! Data Encryption Key (DEK) management and operations.
//!
//! This module provides core functionality for managing Data Encryption Keys,
//! including:
//!
//! - Key generation and persistence
//! - Scope-based key management
//! - Type-safe conversions
//!
//! DEKs are used throughout the library for encrypting and decrypting data
//! while being themselves protected by Customer Managed Keys (CMKs).

pub mod generator;
pub mod persistence;
pub mod scope;

/// A Data Encryption Key (DEK) used for encrypting and decrypting data.
///
/// This type wraps the raw key bytes and provides safe conversion methods
/// to and from byte vectors. DEKs are typically:
///
/// - Generated using cryptographically secure random numbers
/// - Encrypted using a Customer Managed Key (CMK) for storage
/// - Associated with specific data scopes
///
/// # Security
///
/// When handling DEKs:
/// - Protect key material in memory
/// - Clear keys when no longer needed
/// - Use secure persistence methods
/// - Follow key rotation policies
///
/// # Example
/// ```no_run
/// use hyokashi::DEK;
///
/// // Create a DEK from bytes
/// let key_bytes = vec![1, 2, 3, 4];
/// let dek = DEK::from(key_bytes.clone());
///
/// // Convert back to bytes
/// let recovered: Vec<u8> = dek.into();
/// assert_eq!(key_bytes, recovered);
/// ```
#[derive(Debug, Clone)]
pub struct DEK(Vec<u8>);

impl Into<Vec<u8>> for DEK {
    /// Converts the DEK into its raw byte representation.
    ///
    /// This method consumes the DEK and returns the underlying bytes.
    /// Care should be taken to handle the returned bytes securely.
    fn into(self) -> Vec<u8> {
        self.0.clone()
    }
}

impl From<Vec<u8>> for DEK {
    /// Creates a new DEK from a byte vector.
    ///
    /// # Arguments
    ///
    /// * `value` - The raw key bytes to wrap
    ///
    /// # Security
    ///
    /// Ensure the provided bytes:
    /// - Come from a secure source
    /// - Have sufficient entropy
    /// - Are of appropriate length
    fn from(value: Vec<u8>) -> Self {
        Self(value)
    }
}