pub struct Nonce(/* private fields */);
Expand description
A random nonce (“number used once”).
A Nonce
is a cryptographic primitive consisting of a random or
pseudo-random number that is used only once in a cryptographic
communication. Nonces are often used in authentication protocols, encryption
algorithms, and digital signatures to prevent replay attacks and ensure
the uniqueness of encrypted messages.
In this implementation, a Nonce
is a 12-byte random value. The size is
chosen to be sufficiently large to prevent collisions while remaining
efficient for storage and transmission.
§CBOR Serialization
Nonce
implements the CBORTaggedCodable
trait, which means it can be
serialized to and deserialized from CBOR with a specific tag. The tag used
is TAG_NONCE
defined in the tags
module.
§UR Serialization
When serialized as a Uniform Resource (UR), a Nonce
is represented as a
binary blob with the type “nonce”.
§Common Uses
- In authenticated encryption schemes like AES-GCM
- For initializing counters in counter-mode block ciphers
- In challenge-response authentication protocols
- To prevent replay attacks in secure communications
§Examples
Creating a new random nonce:
use bc_components::Nonce;
// Generate a new random nonce
let nonce = Nonce::new();
Creating a nonce from existing data:
use bc_components::Nonce;
// Create a nonce from a byte array
let data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
let nonce = Nonce::from_data(data);
// Access the nonce data
let nonce_data = nonce.data();
Converting to and from hexadecimal representation:
use bc_components::Nonce;
// Create a nonce and convert to hex
let nonce = Nonce::new();
let hex_string = nonce.hex();
// Create a nonce from hex
let nonce_from_hex = Nonce::from_hex(&hex_string);
assert_eq!(nonce, nonce_from_hex);
Implementations§
Source§impl Nonce
impl Nonce
pub const NONCE_SIZE: usize = 12usize
Sourcepub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
Restores a nonce from data.
Trait Implementations§
Source§impl AsRef<Nonce> for Nonce
Provides a self-reference, enabling API consistency with other types.
impl AsRef<Nonce> for Nonce
Provides a self-reference, enabling API consistency with other types.
Source§impl CBORTagged for Nonce
Identifies the CBOR tags used for Nonce serialization.
impl CBORTagged for Nonce
Identifies the CBOR tags used for Nonce serialization.
Source§impl CBORTaggedDecodable for Nonce
Defines how a Nonce is decoded from CBOR.
impl CBORTaggedDecodable for Nonce
Defines how a Nonce is decoded from CBOR.
Source§fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<Self>
fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<Self>
Source§fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
Source§impl CBORTaggedEncodable for Nonce
Defines how a Nonce is encoded as CBOR (as a byte string).
impl CBORTaggedEncodable for Nonce
Defines how a Nonce is encoded as CBOR (as a byte string).
Source§fn untagged_cbor(&self) -> CBOR
fn untagged_cbor(&self) -> CBOR
Source§fn tagged_cbor(&self) -> CBOR
fn tagged_cbor(&self) -> CBOR
Source§impl<'a> From<&'a Nonce> for &'a [u8; 12]
Allows accessing the underlying data as a fixed-size byte array reference.
impl<'a> From<&'a Nonce> for &'a [u8; 12]
Allows accessing the underlying data as a fixed-size byte array reference.
Source§impl From<&Nonce> for Vec<u8>
Converts a Nonce reference into a Vec<u8>
containing the nonce bytes.
impl From<&Nonce> for Vec<u8>
Converts a Nonce reference into a Vec<u8>
containing the nonce bytes.
Source§impl From<Rc<Nonce>> for Nonce
Converts an Rc-wrapped Nonce into a Nonce by cloning the inner value.
impl From<Rc<Nonce>> for Nonce
Converts an Rc-wrapped Nonce into a Nonce by cloning the inner value.
Source§impl TryFrom<CBOR> for Nonce
Enables conversion from CBOR to Nonce, with proper error handling.
impl TryFrom<CBOR> for Nonce
Enables conversion from CBOR to Nonce, with proper error handling.
impl Eq for Nonce
impl StructuralPartialEq for Nonce
Auto Trait Implementations§
impl Freeze for Nonce
impl RefUnwindSafe for Nonce
impl Send for Nonce
impl Sync for Nonce
impl Unpin for Nonce
impl UnwindSafe for Nonce
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CBORDecodable for T
impl<T> CBORDecodable for T
Source§impl<T> CBOREncodable for T
impl<T> CBOREncodable for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Lower case
letters are used (e.g. f9b4ca
)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Upper case
letters are used (e.g. F9B4CA
)