Skip to main content

polymorph_webcrypto_guest/
hmac_sha1.rs

1//! `hmac-sha1` key creation: HMAC over SHA-1, for interoperability with
2//! SHA-1-committed constructions (TOTP, WPA2). HMAC's security rests on
3//! the PRF property, which SHA-1's collision breaks do not reach; prefer
4//! [`hmac_sha2`](crate::hmac_sha2) in new designs.
5
6use crate::{bindings, Error, Mac, MacKeyOptions};
7
8/// Import raw key material as an HMAC-SHA-1 key.
9pub async fn import_key_raw(raw: impl Into<Vec<u8>>, options: MacKeyOptions) -> Result<Mac, Error> {
10    Ok(Mac::from_raw(
11        bindings::hmac_sha1::import_key_raw(raw.into(), options.lower()).await?,
12    ))
13}
14
15/// Import an RFC 7517 `oct` JSON Web Key (as JSON text; `alg` `"HS1"`) as
16/// an HMAC-SHA-1 key.
17pub async fn import_key_jwk(jwk: impl Into<String>, options: MacKeyOptions) -> Result<Mac, Error> {
18    Ok(Mac::from_raw(
19        bindings::hmac_sha1::import_key_jwk(jwk.into(), options.lower()).await?,
20    ))
21}
22
23/// Generate a fresh random HMAC-SHA-1 key. `length` is the key length in
24/// bits; `None` means SHA-1's block size, 512 bits.
25pub async fn generate_key(length: Option<u32>, options: MacKeyOptions) -> Result<Mac, Error> {
26    Ok(Mac::from_raw(
27        bindings::hmac_sha1::generate_key(length, options.lower()).await?,
28    ))
29}
30
31/// Mint an HMAC-SHA-1 key from a parameterized derivation. See
32/// [`hmac_sha2::derive_key`](crate::hmac_sha2::derive_key) for the
33/// `length` and grant contracts.
34pub async fn derive_key(
35    input: &crate::DeriveInput,
36    length: Option<u32>,
37    options: MacKeyOptions,
38) -> Result<Mac, Error> {
39    Ok(Mac::from_raw(
40        bindings::hmac_sha1::derive_key(input.as_raw(), length, options.lower()).await?,
41    ))
42}