trelent-hyok 0.1.12

A Rust library implementing Hold Your Own Key (HYOK) encryption patterns with support for multiple cloud providers
Documentation
//! String-based key generation for testing.
//!
//! This module provides a simple key generation mechanism for testing and
//! development purposes. It converts strings into key material.
//!
//! # Warning
//!
//! This implementation is not cryptographically secure and should only be
//! used for testing purposes. It is only available with the "debug" feature.

#![cfg(feature = "debug")]

use crate::dek::generator::DEKKeyGenerator;
use crate::error::generator::GenerateKeyError;

/// A debug-only key generator that converts a string into bytes for use as a key.
///
/// This generator:
/// - Converts strings to key bytes
/// - Provides deterministic output
/// - Simplifies testing scenarios
/// - Enables reproducible tests
///
/// # Warning
///
/// This generator is intended for testing only and should not be used in
/// production environments because:
/// - It is not cryptographically secure
/// - Key material may be predictable
/// - String conversion may not preserve entropy
/// - Key lengths are not guaranteed
///
/// # Example
/// ```no_run
/// use hyokashi::StringGenerator;
///
/// // Create a test generator
/// let generator = StringGenerator::new("my-test-key".to_string());
/// let key = generator.generate_key()?;
///
/// // Keys are deterministic based on the input string
/// let same_key = generator.generate_key()?;
/// assert_eq!(key, same_key);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug)]
pub struct StringGenerator(pub(crate) String);

impl StringGenerator {
    /// Creates a new `StringGenerator` that will use the provided string as key material.
    ///
    /// # Arguments
    ///
    /// * `gen` - The string to be converted into key bytes.
    pub fn new(gen: String) -> StringGenerator {
        StringGenerator(gen)
    }
}

impl DEKKeyGenerator for StringGenerator {
    /// Generates a key by converting the stored string into bytes.
    ///
    /// # Returns
    ///
    /// The string's UTF-8 bytes.
    fn generate_key(&self) -> Result<Vec<u8>, GenerateKeyError> {
        Ok(self.0.clone().into_bytes())
    }
}