Skip to main content

NonceSequence

Struct NonceSequence 

Source
pub struct NonceSequence<const N: usize> { /* private fields */ }
Expand description

A stateful nonce generator that produces sequentially unique nonces.

§Panics

This type does not panic; all errors are returned as CryptoError.

§Example

use oxicrypto_aead::nonce_seq::Nonce12;

let prefix = [0u8; 4]; // 4-byte prefix for a 12-byte nonce
let mut seq = Nonce12::new(&prefix).unwrap();
let n0 = seq.generate().unwrap();
let n1 = seq.generate().unwrap();
assert_ne!(n0, n1);

Implementations§

Source§

impl<const N: usize> NonceSequence<N>

Source

pub fn new(prefix: &[u8]) -> Result<Self, CryptoError>

Create a new NonceSequence with the given prefix.

prefix must be exactly N - 8 bytes long; otherwise CryptoError::InvalidNonce is returned.

The initial counter value is 0.

Source

pub fn generate(&mut self) -> Result<[u8; N], CryptoError>

Return the next nonce and advance the counter.

§Errors

Returns CryptoError::Internal if the counter has wrapped around u64::MAX (i.e., 2^64 nonces have been generated).

Source

pub fn count(&self) -> u64

Return the current counter value (number of nonces generated so far).

Trait Implementations§

Source§

impl<const N: usize> Debug for NonceSequence<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const N: usize> Freeze for NonceSequence<N>

§

impl<const N: usize> RefUnwindSafe for NonceSequence<N>

§

impl<const N: usize> Send for NonceSequence<N>

§

impl<const N: usize> Sync for NonceSequence<N>

§

impl<const N: usize> Unpin for NonceSequence<N>

§

impl<const N: usize> UnsafeUnpin for NonceSequence<N>

§

impl<const N: usize> UnwindSafe for NonceSequence<N>

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.