Skip to main content

AesGeneratedKey

Struct AesGeneratedKey 

Source
pub struct AesGeneratedKey;
Expand description

AES-256 secure key generator.

AesGeneratedKey provides a simple interface to generate cryptographically secure random keys compatible with AES-256-GCM encryption.

AES-256 requires a key size of:

32 bytes (256 bits)

The key generation uses the operating system’s secure random number generator through getrandom.

§Security

Generated keys are suitable for cryptographic usage:

  • AES-256-GCM encryption
  • File encryption
  • Secure data storage
  • Key wrapping systems

The generated key should be kept secret and should never be exposed publicly.

§Example

Generate an AES-256 key:

use rusty_crypt::AesGeneratedKey;

fn main() -> Result<(), Box<dyn std::error::Error>> {

    let key_generator = AesGeneratedKey;

    let key = key_generator.generate_key()?;

    println!(
        "Generated key size: {} bytes",
        key.len()
    );

    assert_eq!(key.len(), 32);

    Ok(())
}

Implementations§

Source§

impl AesGeneratedKey

Source

pub fn generate_key(&self) -> Result<[u8; 32], CryptoError>

Generates a new random AES-256 encryption key.

This function creates a cryptographically secure random key with a fixed size of 32 bytes.

The generated key can be directly used with:

§Returns

Returns:

[u8; 32]

representing a 256-bit AES key.

§Errors

Returns:

CryptoError::RandomGenerationFailed

if the operating system secure random generator fails.

§Example

Generate a key and encrypt binary data:

use rusty_crypt::{
    AesGeneratedKey,
    AesGcmEncrypt,
    AesGcmGeneratedNonce,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {

    let key =
        AesGeneratedKey.generate_key()?;

    let nonce =
        AesGcmGeneratedNonce;

    let encryptor =
        AesGcmEncrypt::new(&nonce);

    let data =
        b"Secret data";

    let (ciphertext, _) =
        encryptor.encrypt_bytes(
            data,
            &key
        )?;

    println!(
        "Encrypted {} bytes",
        ciphertext.len()
    );

    Ok(())
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.