pub struct Tag { /* private fields */ }
Expand description
A type to represent the Tag
that HMAC returns.
Errors:
An error will be returned if:
slice
is not 64 bytes.
Security:
-
Avoid using
unprotected_as_bytes()
whenever possible, as it breaks all protections that the type implements. -
The trait
PartialEq<&'_ [u8]>
is implemented for this type so that users are not tempted to callunprotected_as_bytes
to compare this sensitive value to a byte slice. The trait is implemented in such a way that the comparison happens in constant time. Thus, users should preferSecretType == &[u8]
overSecretType.unprotected_as_bytes() == &[u8]
. Examples are shown below. The examples apply to any type that implementsPartialEq<&'_ [u8]>
.
use orion::hazardous::mac::hmac::sha512::Tag;
// Initialize an arbitrary, 64-byte tag.
let tag = Tag::from_slice(&[1; 64])?;
// Secure, constant-time comparison with a byte slice
assert_eq!(tag, &[1; 64][..]);
// Secure, constant-time comparison with another Tag
assert_eq!(tag, Tag::from_slice(&[1; 64])?);
Implementations§
source§impl Tag
impl Tag
sourcepub fn from_slice(slice: &[u8]) -> Result<Tag, UnknownCryptoError>
pub fn from_slice(slice: &[u8]) -> Result<Tag, UnknownCryptoError>
Construct from a given byte slice.
sourcepub fn unprotected_as_bytes(&self) -> &[u8] ⓘ
pub fn unprotected_as_bytes(&self) -> &[u8] ⓘ
Return the object as byte slice. Warning: Should not be used unless strictly needed. This breaks protections that the type implements.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Tag
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for Tag
serde
only.This type tries to deserialize as a Vec<u8>
would. If it succeeds, the digest
will be built using Self::from_slice
.
Note that this allocates once to store the referenced bytes on the heap.
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
source§impl PartialEq<&[u8]> for Tag
impl PartialEq<&[u8]> for Tag
source§impl Serialize for Tag
Available on crate feature serde
only.
impl Serialize for Tag
serde
only.This type tries to serialize as a &[u8]
would. Note that the serialized
type likely does not have the same protections that Orion provides, such
as constant-time operations. A good rule of thumb is to only serialize
these types for storage. Don’t operate on the serialized types.