pub struct Key {
pub kty: KeyType,
pub kid: Option<String>,
pub key_use: Option<KeyUse>,
pub key_ops: Option<Vec<KeyOperation>>,
pub alg: Option<Algorithm>,
pub params: KeyParams,
pub x5c: Option<Vec<String>>,
pub x5t: Option<String>,
pub x5t_s256: Option<String>,
pub x5u: Option<String>,
}Expand description
A JSON Web Key (RFC 7517).
Represents a single cryptographic key with its parameters and metadata.
§Examples
use jwk_simple::jwk::{Key, KeyType};
// Parse from JSON
let json = r#"{"kty":"RSA","n":"AQAB","e":"AQAB"}"#;
let jwk: Key = serde_json::from_str(json).unwrap();
// Check key properties
assert_eq!(jwk.kty, KeyType::Rsa);
assert!(jwk.is_public_key_only());Fields§
§kty: KeyTypeThe key type.
kid: Option<String>The key ID.
key_use: Option<KeyUse>The intended use of the key.
key_ops: Option<Vec<KeyOperation>>The permitted operations for the key.
alg: Option<Algorithm>The algorithm intended for use with the key.
params: KeyParamsThe key-type-specific parameters.
x5c: Option<Vec<String>>X.509 certificate chain (base64-encoded DER).
x5t: Option<String>X.509 certificate SHA-1 thumbprint (base64url-encoded).
x5t_s256: Option<String>X.509 certificate SHA-256 thumbprint (base64url-encoded).
x5u: Option<String>X.509 URL.
Implementations§
Source§impl Key
impl Key
Sourcepub fn is_public_key_only(&self) -> bool
pub fn is_public_key_only(&self) -> bool
Returns true if this contains only public key parameters.
Sourcepub fn has_private_key(&self) -> bool
pub fn has_private_key(&self) -> bool
Returns true if this contains private key parameters.
Sourcepub fn validate(&self) -> Result<()>
pub fn validate(&self) -> Result<()>
Validates the JWK.
§Errors
Returns an error if:
- The key parameters are invalid
- The key type doesn’t match the parameters
- The algorithm doesn’t match the key type
- Both
useandkey_opsare specified (RFC 7517 Section 4.3 SHOULD NOT)
Sourcepub fn thumbprint(&self) -> String
pub fn thumbprint(&self) -> String
Calculates the JWK thumbprint (RFC 7638).
The thumbprint is a base64url-encoded SHA-256 hash of the key’s required members.
Sourcepub fn as_symmetric(&self) -> Option<&SymmetricParams>
pub fn as_symmetric(&self) -> Option<&SymmetricParams>
Returns the key as symmetric parameters, if applicable.
Sourcepub fn to_public(&self) -> Option<Key>
pub fn to_public(&self) -> Option<Key>
Extracts only the public key components, removing any private key material.
For symmetric keys, this returns None since symmetric keys don’t have
a separate public component.
§Returns
A new Key containing only the public key parameters, or None for symmetric keys.
§Examples
use jwk_simple::KeySet;
let json = r#"{"keys": [{
"kty": "RSA",
"n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
"e": "AQAB",
"d": "X4cTteJY_gn4FYPsXB8rdXix5vwsg1FLN5E3EaG6RJoVH-HLLKD9M7dx5oo7GURknchnrRweUkC7hT5fJLM0WbFAKNLWY2vv7B6NqXSzUvxT0_YSfqijwp3RTzlBaCxWp4doFk5N2o8Gy_nHNKroADIkJ46pRUohsXywbReAdYaMwFs9tv8d_cPVY3i07a3t8MN6TNwm0dSawm9v47UiCl3Sk5ZiG7xojPLu4sbg1U2jx4IBTNBznbJSzFHK66jT8bgkuqsk0GjskDJk19Z4qwjwbsnn4j2WBii3RL-Us2lGVkY8fkFzme1z0HbIkfz0Y6mqnOYtqc0X4jfcKoAC8Q"
}]}"#;
let jwks: KeySet = serde_json::from_str(json).unwrap();
let private_key = jwks.first().unwrap();
// Extract public key
let public_key = private_key.to_public().expect("RSA keys have public components");
assert!(public_key.is_public_key_only());Source§impl Key
impl Key
Sourcepub fn to_rs256_public_key(&self) -> Result<RS256PublicKey>
Available on crate feature jwt-simple only.
pub fn to_rs256_public_key(&self) -> Result<RS256PublicKey>
jwt-simple only.Sourcepub fn to_rs256_key_pair(&self) -> Result<RS256KeyPair>
Available on crate feature jwt-simple only.
pub fn to_rs256_key_pair(&self) -> Result<RS256KeyPair>
jwt-simple only.Sourcepub fn to_rs384_public_key(&self) -> Result<RS384PublicKey>
Available on crate feature jwt-simple only.
pub fn to_rs384_public_key(&self) -> Result<RS384PublicKey>
jwt-simple only.Converts to an RS384 public key.
Sourcepub fn to_rs384_key_pair(&self) -> Result<RS384KeyPair>
Available on crate feature jwt-simple only.
pub fn to_rs384_key_pair(&self) -> Result<RS384KeyPair>
jwt-simple only.Converts to an RS384 key pair.
Sourcepub fn to_rs512_public_key(&self) -> Result<RS512PublicKey>
Available on crate feature jwt-simple only.
pub fn to_rs512_public_key(&self) -> Result<RS512PublicKey>
jwt-simple only.Converts to an RS512 public key.
Sourcepub fn to_rs512_key_pair(&self) -> Result<RS512KeyPair>
Available on crate feature jwt-simple only.
pub fn to_rs512_key_pair(&self) -> Result<RS512KeyPair>
jwt-simple only.Converts to an RS512 key pair.
Sourcepub fn to_ps256_public_key(&self) -> Result<PS256PublicKey>
Available on crate feature jwt-simple only.
pub fn to_ps256_public_key(&self) -> Result<PS256PublicKey>
jwt-simple only.Converts to a PS256 public key.
Sourcepub fn to_ps256_key_pair(&self) -> Result<PS256KeyPair>
Available on crate feature jwt-simple only.
pub fn to_ps256_key_pair(&self) -> Result<PS256KeyPair>
jwt-simple only.Converts to a PS256 key pair.
Sourcepub fn to_ps384_public_key(&self) -> Result<PS384PublicKey>
Available on crate feature jwt-simple only.
pub fn to_ps384_public_key(&self) -> Result<PS384PublicKey>
jwt-simple only.Converts to a PS384 public key.
Sourcepub fn to_ps384_key_pair(&self) -> Result<PS384KeyPair>
Available on crate feature jwt-simple only.
pub fn to_ps384_key_pair(&self) -> Result<PS384KeyPair>
jwt-simple only.Converts to a PS384 key pair.
Sourcepub fn to_ps512_public_key(&self) -> Result<PS512PublicKey>
Available on crate feature jwt-simple only.
pub fn to_ps512_public_key(&self) -> Result<PS512PublicKey>
jwt-simple only.Converts to a PS512 public key.
Sourcepub fn to_ps512_key_pair(&self) -> Result<PS512KeyPair>
Available on crate feature jwt-simple only.
pub fn to_ps512_key_pair(&self) -> Result<PS512KeyPair>
jwt-simple only.Converts to a PS512 key pair.
Sourcepub fn to_es256_public_key(&self) -> Result<ES256PublicKey>
Available on crate feature jwt-simple only.
pub fn to_es256_public_key(&self) -> Result<ES256PublicKey>
jwt-simple only.Converts to an ES256 public key.
Sourcepub fn to_es256_key_pair(&self) -> Result<ES256KeyPair>
Available on crate feature jwt-simple only.
pub fn to_es256_key_pair(&self) -> Result<ES256KeyPair>
jwt-simple only.Converts to an ES256 key pair.
Sourcepub fn to_es384_public_key(&self) -> Result<ES384PublicKey>
Available on crate feature jwt-simple only.
pub fn to_es384_public_key(&self) -> Result<ES384PublicKey>
jwt-simple only.Converts to an ES384 public key.
Sourcepub fn to_es384_key_pair(&self) -> Result<ES384KeyPair>
Available on crate feature jwt-simple only.
pub fn to_es384_key_pair(&self) -> Result<ES384KeyPair>
jwt-simple only.Converts to an ES384 key pair.
Sourcepub fn to_es256k_public_key(&self) -> Result<ES256kPublicKey>
Available on crate feature jwt-simple only.
pub fn to_es256k_public_key(&self) -> Result<ES256kPublicKey>
jwt-simple only.Converts to an ES256k public key.
Sourcepub fn to_es256k_key_pair(&self) -> Result<ES256kKeyPair>
Available on crate feature jwt-simple only.
pub fn to_es256k_key_pair(&self) -> Result<ES256kKeyPair>
jwt-simple only.Converts to an ES256k key pair.
Sourcepub fn to_ed25519_public_key(&self) -> Result<Ed25519PublicKey>
Available on crate feature jwt-simple only.
pub fn to_ed25519_public_key(&self) -> Result<Ed25519PublicKey>
jwt-simple only.Converts to an Ed25519 public key.
Sourcepub fn to_ed25519_key_pair(&self) -> Result<Ed25519KeyPair>
Available on crate feature jwt-simple only.
pub fn to_ed25519_key_pair(&self) -> Result<Ed25519KeyPair>
jwt-simple only.Converts to an Ed25519 key pair.
Sourcepub fn to_hs256_key(&self) -> Result<HS256Key>
Available on crate feature jwt-simple only.
pub fn to_hs256_key(&self) -> Result<HS256Key>
jwt-simple only.Converts to an HS256 key.
Sourcepub fn to_hs384_key(&self) -> Result<HS384Key>
Available on crate feature jwt-simple only.
pub fn to_hs384_key(&self) -> Result<HS384Key>
jwt-simple only.Converts to an HS384 key.
Sourcepub fn to_hs512_key(&self) -> Result<HS512Key>
Available on crate feature jwt-simple only.
pub fn to_hs512_key(&self) -> Result<HS512Key>
jwt-simple only.Converts to an HS512 key.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Key
impl<'de> Deserialize<'de> for Key
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl FromIterator<Key> for KeySet
impl FromIterator<Key> for KeySet
Source§impl TryFrom<&Key> for ES256KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for ES256KeyPair
jwt-simple only.Source§impl TryFrom<&Key> for ES256PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for ES256PublicKey
jwt-simple only.Source§impl TryFrom<&Key> for ES256kKeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for ES256kKeyPair
jwt-simple only.Source§impl TryFrom<&Key> for ES256kPublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for ES256kPublicKey
jwt-simple only.Source§impl TryFrom<&Key> for ES384KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for ES384KeyPair
jwt-simple only.Source§impl TryFrom<&Key> for ES384PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for ES384PublicKey
jwt-simple only.Source§impl TryFrom<&Key> for Ed25519KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for Ed25519KeyPair
jwt-simple only.Source§impl TryFrom<&Key> for Ed25519PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for Ed25519PublicKey
jwt-simple only.Source§impl TryFrom<&Key> for PS256KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for PS256KeyPair
jwt-simple only.Source§impl TryFrom<&Key> for PS256PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for PS256PublicKey
jwt-simple only.Source§impl TryFrom<&Key> for PS384KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for PS384KeyPair
jwt-simple only.Source§impl TryFrom<&Key> for PS384PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for PS384PublicKey
jwt-simple only.Source§impl TryFrom<&Key> for PS512KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for PS512KeyPair
jwt-simple only.Source§impl TryFrom<&Key> for PS512PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for PS512PublicKey
jwt-simple only.Source§impl TryFrom<&Key> for RS256KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for RS256KeyPair
jwt-simple only.Source§impl TryFrom<&Key> for RS256PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for RS256PublicKey
jwt-simple only.Source§impl TryFrom<&Key> for RS384KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for RS384KeyPair
jwt-simple only.Source§impl TryFrom<&Key> for RS384PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for RS384PublicKey
jwt-simple only.Source§impl TryFrom<&Key> for RS512KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for RS512KeyPair
jwt-simple only.Source§impl TryFrom<&Key> for RS512PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<&Key> for RS512PublicKey
jwt-simple only.Source§impl TryFrom<Key> for ES256KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for ES256KeyPair
jwt-simple only.Source§impl TryFrom<Key> for ES256PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for ES256PublicKey
jwt-simple only.Source§impl TryFrom<Key> for ES256kKeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for ES256kKeyPair
jwt-simple only.Source§impl TryFrom<Key> for ES256kPublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for ES256kPublicKey
jwt-simple only.Source§impl TryFrom<Key> for ES384KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for ES384KeyPair
jwt-simple only.Source§impl TryFrom<Key> for ES384PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for ES384PublicKey
jwt-simple only.Source§impl TryFrom<Key> for Ed25519KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for Ed25519KeyPair
jwt-simple only.Source§impl TryFrom<Key> for Ed25519PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for Ed25519PublicKey
jwt-simple only.Source§impl TryFrom<Key> for PS256KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for PS256KeyPair
jwt-simple only.Source§impl TryFrom<Key> for PS256PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for PS256PublicKey
jwt-simple only.Source§impl TryFrom<Key> for PS384KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for PS384KeyPair
jwt-simple only.Source§impl TryFrom<Key> for PS384PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for PS384PublicKey
jwt-simple only.Source§impl TryFrom<Key> for PS512KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for PS512KeyPair
jwt-simple only.Source§impl TryFrom<Key> for PS512PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for PS512PublicKey
jwt-simple only.Source§impl TryFrom<Key> for RS256KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for RS256KeyPair
jwt-simple only.Source§impl TryFrom<Key> for RS256PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for RS256PublicKey
jwt-simple only.Source§impl TryFrom<Key> for RS384KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for RS384KeyPair
jwt-simple only.Source§impl TryFrom<Key> for RS384PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for RS384PublicKey
jwt-simple only.Source§impl TryFrom<Key> for RS512KeyPair
Available on crate feature jwt-simple only.
impl TryFrom<Key> for RS512KeyPair
jwt-simple only.Source§impl TryFrom<Key> for RS512PublicKey
Available on crate feature jwt-simple only.
impl TryFrom<Key> for RS512PublicKey
jwt-simple only.