pub struct HKDF;
Expand description
HMAC-based Key Derivation Function (HKDF) implementation using SHA-256.
HKDF is a key derivation function based on HMAC, standardized in RFC 5869. It is used to derive one or more cryptographically strong keys from input keying material.
The HKDF process consists of two stages:
- Extract: Takes input keying material and an optional salt, and produces a pseudorandom key (PRK)
- Expand: Takes the PRK, optional context information, and desired output length to generate output keying material
§Examples
Basic usage:
// Extract a pseudorandom key from input keying material using a salt
let prk = hmac_sha256::HKDF::extract(b"salt value", b"input key material");
// Expand the PRK into output keying material of desired length
let mut okm = [0u8; 64]; // 64 bytes of output keying material
hmac_sha256::HKDF::expand(&mut okm, prk, b"application info");
Implementations§
Source§impl HKDF
impl HKDF
Sourcepub fn extract(salt: impl AsRef<[u8]>, ikm: impl AsRef<[u8]>) -> [u8; 32]
pub fn extract(salt: impl AsRef<[u8]>, ikm: impl AsRef<[u8]>) -> [u8; 32]
Performs the HKDF-Extract function (first stage of HKDF).
Extracts a pseudorandom key from the input keying material using the optional salt.
§Arguments
salt
- Optional salt value (a non-secret random value)ikm
- Input keying material (the secret input)
§Returns
A 32-byte pseudorandom key
§Example
let prk = hmac_sha256::HKDF::extract(b"salt value", b"input key material");
Sourcepub fn expand(out: &mut [u8], prk: impl AsRef<[u8]>, info: impl AsRef<[u8]>)
pub fn expand(out: &mut [u8], prk: impl AsRef<[u8]>, info: impl AsRef<[u8]>)
Performs the HKDF-Expand function (second stage of HKDF).
Expands the pseudorandom key into output keying material of the desired length.
§Arguments
out
- Buffer to receive the output keying materialprk
- Pseudorandom key (from the extract step)info
- Optional context and application specific information
§Panics
Panics if the requested output length is greater than 255 * 32 bytes (8160 bytes).
§Example
let prk = hmac_sha256::HKDF::extract(b"salt", b"input key material");
let mut okm = [0u8; 64]; // 64 bytes of output keying material
hmac_sha256::HKDF::expand(&mut okm, prk, b"context info");
Auto Trait Implementations§
impl Freeze for HKDF
impl RefUnwindSafe for HKDF
impl Send for HKDF
impl Sync for HKDF
impl Unpin for HKDF
impl UnwindSafe for HKDF
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more