pub struct Tag(/* private fields */);Expand description
An authentication tag
This represents an authentication tag computed for a message using a secret key. The tag can be publicly shared and later verified to ensure the authenticity of a message.
Implementations§
Source§impl Tag
impl Tag
Sourcepub fn from_slice(slice: &[u8]) -> Result<Self>
pub fn from_slice(slice: &[u8]) -> Result<Self>
Creates a tag from a byte slice
This function creates a tag from an existing byte slice, which must be exactly
BYTES (32) bytes long. This is useful when you receive a tag from another party
and need to verify it.
§Arguments
slice- Byte slice of exactlyBYTES(32) bytes length
§Returns
Result<Self>- A new tag or an error if the input is invalid
§Errors
Returns an error if the input is not exactly BYTES bytes long
§Example
use libsodium_rs as sodium;
use sodium::crypto_auth;
use sodium::ensure_init;
// Initialize libsodium
ensure_init().expect("Failed to initialize libsodium");
// Create a tag from existing bytes
let tag_bytes = [0x42; crypto_auth::BYTES]; // In a real application, this would be a real tag
let tag = crypto_auth::Tag::from_slice(&tag_bytes).unwrap();Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns a reference to the tag as a byte slice
This method provides access to the raw bytes of the tag, which can be useful when you need to transmit the tag or store it.
§Returns
&[u8]- Reference to the tag bytes
§Example
use libsodium_rs as sodium;
use sodium::crypto_auth;
use sodium::ensure_init;
// Initialize libsodium
ensure_init().expect("Failed to initialize libsodium");
// Generate a random key
let key = crypto_auth::Key::generate().unwrap();
// Compute authentication tag for a message
let message = b"Hello, world!";
let tag = crypto_auth::auth(message, &key).unwrap();
// Get the raw bytes of the tag
let tag_bytes = tag.as_bytes();
assert_eq!(tag_bytes.len(), crypto_auth::BYTES);Trait Implementations§
impl Eq for Tag
impl StructuralPartialEq for Tag
Auto Trait Implementations§
impl Freeze for Tag
impl RefUnwindSafe for Tag
impl Send for Tag
impl Sync for Tag
impl Unpin for Tag
impl UnsafeUnpin for Tag
impl UnwindSafe for Tag
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
Mutably borrows from an owned value. Read more