Nonce Struct
Definition
- Namespace
- Bodu.Security.Cryptography
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
- Source
- Nonce.cs
Represents a number-used-once supplied to an authenticated-encryption or stream-cipher operation.
public readonly struct Nonce : IEquatable<Nonce>
- Implements
- Inherited Members
- Extension Methods
Examples
// Generate a fresh 12-byte nonce for an AES-GCM operation; it is unique per message, not secret.
Nonce nonce = Nonce.Random(12);
using var aes = new AesGcm(key, tagSizeInBytes: 16);
aes.Encrypt(nonce.AsSpan(), plaintext, ciphertext, tag);
// The nonce travels with the ciphertext; reconstruct it on the receiving side to decrypt.
Nonce received = Nonce.FromBytes(wireNonceBytes);
Remarks
A nonce is unique per operation under a given key but is not secret; it typically travels alongside the ciphertext. Using a dedicated type keeps nonces from being confused with keys, salts, or authentication tags in APIs that would otherwise accept several look-alike byte buffers.
Nonce carries its bytes by defensive copy, and the default instance (default(Nonce)) is the
empty value: Length is 0 and IsEmpty is true.
This type does not enforce uniqueness; callers remain responsible for never reusing a nonce with the same key. Random(int) draws from a cryptographically secure generator, which is appropriate for nonce sizes where random collision is negligible (for example the 24-byte XChaCha20 nonce).
Properties
IsEmpty
Gets a value indicating whether the nonce is empty.
public bool IsEmpty { get; }
Property Value
Length
Gets the number of bytes in the nonce.
public int Length { get; }
Property Value
- int
The nonce length in bytes, or
0for the empty value.
Methods
AsSpan()
Returns a read-only view over the nonce bytes.
public ReadOnlySpan<byte> AsSpan()
Returns
- ReadOnlySpan<byte>
A read-only span over the value; empty for the empty value.
Equals(Nonce)
Determines whether this nonce equals another.
public bool Equals(Nonce other)
Parameters
otherNonceThe nonce to compare against.
Returns
Remarks
The comparison runs through
FixedTimeEquals(ReadOnlySpan<byte>, ReadOnlySpan<byte>) for a uniform,
content-independent duration across the secret-bearing value types, though a nonce is not itself a secret. A
length mismatch returns false immediately. No dedicated FixedTimeEquals member is
offered: a nonce is a public, non-repeating value and is not verified against attacker-supplied input, so
exposing one would wrongly imply a secret-comparison contract.
Equals(object?)
Determines whether this nonce equals the specified object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare against.
Returns
FromBytes(ReadOnlySpan<byte>)
Creates a Nonce from the provided bytes.
public static Nonce FromBytes(ReadOnlySpan<byte> value)
Parameters
valueReadOnlySpan<byte>The nonce bytes to copy. An empty span yields the empty value.
Returns
GetHashCode()
Returns a hash code computed over the nonce bytes.
public override int GetHashCode()
Returns
- int
A hash code consistent with Equals(Nonce).
Random(int)
Creates a Nonce of the specified length filled with cryptographically secure random bytes.
public static Nonce Random(int length)
Parameters
lengthintThe number of random bytes to generate.
Returns
Exceptions
- ArgumentOutOfRangeException
length≤ 0.
ToArray()
Copies the nonce bytes into a new array.
public byte[] ToArray()
Returns
- byte[]
A new array containing the nonce bytes; an empty array for the empty value.
ToString()
Returns the lowercase hexadecimal representation of the nonce.
public override string ToString()
Returns
Remarks
Nonces are not secrets, so the content is intentionally included in the string representation.
Operators
operator ==(Nonce, Nonce)
Determines whether two nonces are equal.
public static bool operator ==(Nonce left, Nonce right)
Parameters
Returns
operator !=(Nonce, Nonce)
Determines whether two nonces are not equal.
public static bool operator !=(Nonce left, Nonce right)
Parameters
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |