Skip to main content

crypto_csprng/
types.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use zeroize::{Zeroize, ZeroizeOnDrop};
6
7use crate::constants::{AEAD_NONCE_12_LENGTH, ARGON2_SALT_16_LENGTH, ARGON2_SALT_32_LENGTH};
8
9/// A randomly generated 12-byte AEAD nonce.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct AeadNonce12([u8; AEAD_NONCE_12_LENGTH]);
12
13impl AeadNonce12 {
14    /// Returns a reference to the raw nonce bytes.
15    pub const fn as_bytes(&self) -> &[u8; AEAD_NONCE_12_LENGTH] {
16        &self.0
17    }
18
19    pub(crate) fn from_array(bytes: [u8; AEAD_NONCE_12_LENGTH]) -> Self {
20        Self(bytes)
21    }
22}
23
24/// A randomly generated 16-byte Argon2 salt.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26pub struct Argon2Salt16([u8; ARGON2_SALT_16_LENGTH]);
27
28impl Argon2Salt16 {
29    /// Returns a reference to the raw salt bytes.
30    pub const fn as_bytes(&self) -> &[u8; ARGON2_SALT_16_LENGTH] {
31        &self.0
32    }
33
34    pub(crate) fn from_array(bytes: [u8; ARGON2_SALT_16_LENGTH]) -> Self {
35        Self(bytes)
36    }
37}
38
39/// A randomly generated 32-byte Argon2 salt.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
41pub struct Argon2Salt32([u8; ARGON2_SALT_32_LENGTH]);
42
43impl Argon2Salt32 {
44    /// Returns a reference to the raw salt bytes.
45    pub const fn as_bytes(&self) -> &[u8; ARGON2_SALT_32_LENGTH] {
46        &self.0
47    }
48
49    pub(crate) fn from_array(bytes: [u8; ARGON2_SALT_32_LENGTH]) -> Self {
50        Self(bytes)
51    }
52}
53
54/// A buffer of `N` randomly generated bytes.
55///
56/// Zeroizes its bytes on drop.
57#[derive(Zeroize, ZeroizeOnDrop)]
58pub struct RandomBytes<const N: usize> {
59    bytes: [u8; N],
60}
61
62impl<const N: usize> RandomBytes<N> {
63    /// Returns a reference to the raw random bytes.
64    pub const fn as_bytes(&self) -> &[u8; N] {
65        &self.bytes
66    }
67
68    /// Consumes the buffer, returning the owned random bytes.
69    ///
70    /// The returned array is no longer zeroized on drop by this type.
71    pub fn into_bytes(self) -> [u8; N] {
72        self.bytes
73    }
74
75    pub(crate) fn from_array(bytes: [u8; N]) -> Self {
76        Self { bytes }
77    }
78}