Expand description
§HMAC authentication
HmacSha256, HmacSha512, and HmacSha512256 provide Rustaceous
wrappers for libsodium’s direct HMAC authentication variants.
HMAC computes a fixed-size authentication tag for a message using a shared secret key. Anyone with the same key can recompute the tag and verify that the message was produced by someone who knew the key and that the message was not changed. HMAC does not encrypt the message.
Use these types when:
- you need one of libsodium’s direct
crypto_auth_hmacsha*variants - two parties already share the same secret key
- the message can be public, but tampering must be detected
HmacSha512256 matches libsodium’s default crypto_auth
construction. HmacSha256 and HmacSha512 are available for protocol
compatibility when those exact algorithms are required.
§Rustaceous API example
use dryoc::hmac::{HmacSha256, HmacSha256Key};
use dryoc::types::*;
let key = HmacSha256Key::generate();
let message = b"Uneasy lies the head that wears a crown.";
let mac = HmacSha256::compute_to_vec(key.clone(), message);
HmacSha256::compute_and_verify(&mac, key, message).expect("verify failed");The concrete authenticators are type aliases over Hmac and can also be
used through HmacVariant in generic code.
§Incremental interface
use dryoc::hmac::{HmacSha512256, HmacSha512256Key};
use dryoc::types::*;
let key = HmacSha512256Key::generate();
let mut auth = HmacSha512256::new(key.clone());
auth.update(b"Though she be but little, ");
auth.update(b"she is fierce.");
let mac = auth.finalize_to_vec();
let mut verifier = HmacSha512256::new(key);
verifier.update(b"Though she be but little, ");
verifier.update(b"she is fierce.");
verifier.verify(&mac).expect("verify failed");§Generic HMAC variants
use dryoc::constants::{CRYPTO_AUTH_HMACSHA256_BYTES, CRYPTO_AUTH_HMACSHA256_KEYBYTES};
use dryoc::hmac::{Hmac, HmacSha256, HmacSha256Key, HmacSha256Variant, HmacVariant};
use dryoc::types::*;
fn authenticate<Variant, const KEY_LENGTH: usize, const MAC_LENGTH: usize>(
key: StackByteArray<KEY_LENGTH>,
input: &[u8],
) -> Vec<u8>
where
Variant: HmacVariant<KEY_LENGTH, MAC_LENGTH>,
{
Hmac::<Variant, KEY_LENGTH, MAC_LENGTH>::compute_to_vec(key, input)
}
let key = HmacSha256Key::generate();
let message = b"The quality of mercy is not strained.";
let generic_mac = authenticate::<
HmacSha256Variant,
CRYPTO_AUTH_HMACSHA256_KEYBYTES,
CRYPTO_AUTH_HMACSHA256_BYTES,
>(key.clone(), message);
let concrete_mac = HmacSha256::compute_to_vec(key, message);
assert_eq!(generic_mac, concrete_mac);Modules§
- protected
protected - Protected memory type aliases for HMAC
Structs§
- Hmac
- Rustaceous HMAC authenticator for a specific
HmacVariant. - Hmac
Sha256 Variant - HMAC-SHA-256 algorithm marker.
- Hmac
Sha512 Variant - HMAC-SHA-512 algorithm marker.
- Hmac
Sha512256 Variant - HMAC-SHA-512-256 algorithm marker.
Traits§
- Hmac
Variant - HMAC algorithm variant used by
Hmac.
Type Aliases§
- Hmac
Sha256 - Rustaceous HMAC-SHA-256 authenticator.
- Hmac
Sha512 - Rustaceous HMAC-SHA-512 authenticator.
- Hmac
Sha256 Key - Stack-allocated key for HMAC-SHA-256.
- Hmac
Sha256 Mac - Stack-allocated message authentication code for HMAC-SHA-256.
- Hmac
Sha512 Key - Stack-allocated key for HMAC-SHA-512.
- Hmac
Sha512 Mac - Stack-allocated message authentication code for HMAC-SHA-512.
- Hmac
Sha512256 - Rustaceous HMAC-SHA-512-256 authenticator.
- Hmac
Sha512256 Key - Stack-allocated key for HMAC-SHA-512-256.
- Hmac
Sha512256 Mac - Stack-allocated message authentication code for HMAC-SHA-512-256.