Skip to main content

Module crypto_auth_hmacsha256

Module crypto_auth_hmacsha256 

Source
Expand description

§HMAC-SHA-256 authentication

Implements libsodium’s crypto_auth_hmacsha256_* functions.

HMAC-SHA-256 authenticates a message with a shared secret key and writes a 32-byte tag. Verification recomputes the tag and compares it in constant time. The message is not encrypted, and the same key must be available to both the sender and verifier.

use dryoc::classic::crypto_auth_hmacsha256::*;

let key = crypto_auth_hmacsha256_keygen();
let message = b"What's past is prologue.";

let mut mac = Mac::default();
crypto_auth_hmacsha256(&mut mac, message, &key);
crypto_auth_hmacsha256_verify(&mac, message, &key).expect("verify failed");
crypto_auth_hmacsha256_verify(&mac, b"invalid", &key).expect_err("verify should fail");

The incremental interface produces the same MAC as the one-shot interface:

use dryoc::classic::crypto_auth_hmacsha256::*;

let key = crypto_auth_hmacsha256_keygen();
let mut one_shot = Mac::default();
crypto_auth_hmacsha256(&mut one_shot, b"Parting is such sweet sorrow.", &key);

let mut state = crypto_auth_hmacsha256_init(&key);
crypto_auth_hmacsha256_update(&mut state, b"Parting is such ");
crypto_auth_hmacsha256_update(&mut state, b"sweet sorrow.");
let mut streaming = Mac::default();
crypto_auth_hmacsha256_final(state, &mut streaming);

assert_eq!(one_shot, streaming);

Structs§

HmacSha256State
Internal state for HMAC-SHA-256.

Functions§

crypto_auth_hmacsha256
Authenticates message using key, and places the result into mac.
crypto_auth_hmacsha256_final
Finalizes HMAC-SHA-256 and places the result into output.
crypto_auth_hmacsha256_init
Initializes the incremental interface for HMAC-SHA-256.
crypto_auth_hmacsha256_keygen
Generates a random key for HMAC-SHA-256.
crypto_auth_hmacsha256_update
Updates state for HMAC-SHA-256 with input.
crypto_auth_hmacsha256_verify
Verifies that mac is the correct authenticator for message using key.

Type Aliases§

Key
Key for HMAC-SHA-256 message authentication.
Mac
Message authentication code type for HMAC-SHA-256.