reallyme-crypto-csprng 0.2.0

Cryptographically secure random byte generation for ReallyMe Crypto.
Documentation
// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
//
// SPDX-License-Identifier: Apache-2.0

use zeroize::{Zeroize, ZeroizeOnDrop};

use crate::constants::{AEAD_NONCE_12_LENGTH, ARGON2_SALT_16_LENGTH, ARGON2_SALT_32_LENGTH};

/// A randomly generated 12-byte AEAD nonce.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AeadNonce12([u8; AEAD_NONCE_12_LENGTH]);

impl AeadNonce12 {
    /// Returns a reference to the raw nonce bytes.
    pub const fn as_bytes(&self) -> &[u8; AEAD_NONCE_12_LENGTH] {
        &self.0
    }

    pub(crate) fn from_array(bytes: [u8; AEAD_NONCE_12_LENGTH]) -> Self {
        Self(bytes)
    }
}

/// A randomly generated 16-byte Argon2 salt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Argon2Salt16([u8; ARGON2_SALT_16_LENGTH]);

impl Argon2Salt16 {
    /// Returns a reference to the raw salt bytes.
    pub const fn as_bytes(&self) -> &[u8; ARGON2_SALT_16_LENGTH] {
        &self.0
    }

    pub(crate) fn from_array(bytes: [u8; ARGON2_SALT_16_LENGTH]) -> Self {
        Self(bytes)
    }
}

/// A randomly generated 32-byte Argon2 salt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Argon2Salt32([u8; ARGON2_SALT_32_LENGTH]);

impl Argon2Salt32 {
    /// Returns a reference to the raw salt bytes.
    pub const fn as_bytes(&self) -> &[u8; ARGON2_SALT_32_LENGTH] {
        &self.0
    }

    pub(crate) fn from_array(bytes: [u8; ARGON2_SALT_32_LENGTH]) -> Self {
        Self(bytes)
    }
}

/// A buffer of `N` randomly generated bytes.
///
/// Zeroizes its bytes on drop.
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct RandomBytes<const N: usize> {
    bytes: [u8; N],
}

impl<const N: usize> RandomBytes<N> {
    /// Returns a reference to the raw random bytes.
    pub const fn as_bytes(&self) -> &[u8; N] {
        &self.bytes
    }

    /// Consumes the buffer, returning the owned random bytes.
    ///
    /// The returned array is no longer zeroized on drop by this type.
    pub fn into_bytes(self) -> [u8; N] {
        self.bytes
    }

    pub(crate) fn from_array(bytes: [u8; N]) -> Self {
        Self { bytes }
    }
}