Module dryoc::classic::crypto_onetimeauth
source · Expand description
§One-time authentication
Implements one-time authentication using the Poly1305 algorithm, compatible
with libsodium’s crypto_onetimeauth_* functions.
§Classic API single-part example
use base64::engine::general_purpose;
use base64::Engine as _;
use dryoc::classic::crypto_onetimeauth::{
crypto_onetimeauth, crypto_onetimeauth_keygen, crypto_onetimeauth_verify, Mac,
};
let key = crypto_onetimeauth_keygen();
let mut mac = Mac::default();
crypto_onetimeauth(&mut mac, b"Data to authenticate", &key);
// This should be valid
crypto_onetimeauth_verify(&mac, b"Data to authenticate", &key).expect("failed to authenticate");
// This should not be valid
crypto_onetimeauth_verify(&mac, b"Invalid data", &key).expect_err("should not authenticate");§Classic API multi-part example
use base64::engine::general_purpose;
use base64::Engine as _;
use dryoc::classic::crypto_onetimeauth::{
crypto_onetimeauth_final, crypto_onetimeauth_init, crypto_onetimeauth_keygen,
crypto_onetimeauth_update, crypto_onetimeauth_verify, Mac,
};
let key = crypto_onetimeauth_keygen();
let mut mac = Mac::default();
let mut state = crypto_onetimeauth_init(&key);
crypto_onetimeauth_update(&mut state, b"Multi-part");
crypto_onetimeauth_update(&mut state, b"data");
crypto_onetimeauth_final(state, &mut mac);
// This should be valid
crypto_onetimeauth_verify(&mac, b"Multi-partdata", &key).expect("failed to authenticate");
// This should not be valid
crypto_onetimeauth_verify(&mac, b"Invalid data", &key).expect_err("should not authenticate");Structs§
- Internal state for
crypto_onetimeauth.
Functions§
- Authenticates
messageusingkey, and places the result intomac.keyshould only be used once. - Finalizes the message authentication code for
state, and places the result intooutput. - Initialize the incremental interface for Poly1305-based one-time authentication, using
key. Returns a state struct which is required for subsequent calls tocrypto_onetimeauth_updateandcrypto_onetimeauth_final. The key should only be used once. - Generates a random key using
copy_randombytes, suitable for use withcrypto_onetimeauth_initandcrypto_onetimeauth. The key should only be used once. - Updates
statefor the one-time authentication function, based oninput. - Verifies that
macis the correct authenticator formessageusingkey. ReturnsOk(())if the message authentication code is valid.
Type Aliases§
- Key type for use with one-time authentication.
- Message authentication code type for use with one-time authentication.