Module jwt_compact::jwk

source ·
Expand description

Basic support of JSON Web Keys (JWK).

The functionality defined in this module allows converting between the generic JWK format and key presentation specific for the crypto backend. JsonWebKeys can be (de)serialized using serde infrastructure, and can be used to compute key thumbprint as per RFC 7638.

§Examples

use jwt_compact::{alg::Hs256Key, jwk::JsonWebKey};
use sha2::Sha256;

// Load a key from the JWK presentation.
let json_str = r#"
    { "kty": "oct", "k": "t-bdv41MJXExXnpquHBuDn7n1YGyX7gLQchVHAoNu50" }
"#;
let jwk: JsonWebKey<'_> = serde_json::from_str(json_str)?;
let key = Hs256Key::try_from(&jwk)?;

// Convert `key` back to JWK.
let jwk_from_key = JsonWebKey::from(&key);
assert_eq!(jwk_from_key, jwk);
println!("{}", serde_json::to_string(&jwk)?);

// Compute the key thumbprint.
let thumbprint = jwk_from_key.thumbprint::<Sha256>();

Structs§

Enums§