Skip to main content

Module mac_factory

Module mac_factory 

Source
Expand description

MAC factory for creating instances of algorithms that implement the MAC trait.

As with all Factory objects, this implements constructions from strings and defaults, and returns a MACFactory object which itself implements the MAC trait as a pass-through to the underlying algorithm.

Example usage: Generating and verifying a MAC value for a given piece of data:

use core_interface::key_material::{KeyMaterial256, KeyType};
use core_interface::traits::MAC;
use encoders::hex;
use factory::AlgorithmFactory;
use factory::mac_factory::MACFactory;

let data = b"Hi There!";
let key = KeyMaterial256::from_bytes_as_type(
        // Note: This would be a bad key to use in a production application!
        // But we'll hard-code a silly key for demonstration purposes.
        &hex::decode("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b").unwrap(),
        KeyType::MACKey,
    ).unwrap();
let hmac = MACFactory::new(hmac::HMAC_SHA3_256_NAME).unwrap();

// Generate the MAC value
let mac_value: Vec<u8> = hmac.mac(&key, data).unwrap();

// Verify the MAC value
match hmac.verify(&key, data, &mac_value,) {
    Ok(()) => println!("MAC verified successfully!"),
    Err(e) => println!("MAC verification failed"),
}

You can equivalently construct an instance of MACFactory by string instead of using the constant:

use factory::AlgorithmFactory;
use factory::mac_factory::MACFactory;

let hmac = MACFactory::new("HMAC-SHA256").unwrap();

Or if you don’t particularly care which algorithm is used, you can use the built-in default:

use factory::AlgorithmFactory;
use factory::mac_factory::MACFactory;

let hmac = MACFactory::default();

Enums§

MACFactory
MACFactory deviates from the usual AlgorithmFactory trait because MAC objects do not have a no-arg constructor; instead they have a constructor that takes a KeyMaterial and can return an error.

Constants§

DEFAULT_128BIT_MAC_NAME
DEFAULT_256BIT_MAC_NAME
DEFAULT_MAC_NAME