Skip to main content

Key

Enum Key 

Source
pub enum Key<'a> {
Show 15 variants Secret(SecretKey<'a>), Ed25519Public(PublicKey), Ed25519Secret(SecretKey), P256Public(PublicKey), P256Secret(SecretKey), P384Public(PublicKey), P521Public(PublicKey), P521Secret(SecretKey), Rsa(RsaPublicKey), MlDsa44Public(MlDsa44PublicKey), MlDsa44Secret(MlDsa44SecretKey), MlDsa65Public(MlDsa65PublicKey), MlDsa65Secret(Box<MlDsa65SecretKey>), MlDsa87Public(MlDsa87PublicKey), MlDsa87Secret(Box<MlDsa87SecretKey>),
}
Expand description

A JWK decoded into a concrete cryptographic key.

This is the entry point when the key type is only known at runtime, for example after fetching a JWKS document. Key::try_from inspects the JWK’s kty (and crv) to select the right variant, so callers do not need to know in advance whether the key is RSA, EC, OKP, …

When a JWK carries both public and secret material (for example one produced from a secret key), the secret variant is preferred.

Key implements both Signer and Verifier, so a decoded key can be passed directly to sign or parse_and_verify. Public-only keys return Error::InvalidKey when signing.

§Errors

Key::try_from returns Error::InvalidKey if the JWK’s key material is inconsistent with its declared algorithm, if the curve or algorithm is unsupported, or if the underlying key bytes are invalid.

Variants§

§

Secret(SecretKey<'a>)

Symmetric key for the BLAKE3, HS256, HS384, and HS512 algorithms.

§

Ed25519Public(PublicKey)

Ed25519 public key for EdDSA.

§

Ed25519Secret(SecretKey)

Ed25519 secret key for EdDSA.

§

P256Public(PublicKey)

P-256 public key for ES256.

§

P256Secret(SecretKey)

P-256 secret key for ES256.

§

P384Public(PublicKey)

P-384 public key for ES384.

§

P521Public(PublicKey)

P-521 public key for ES512.

§

P521Secret(SecretKey)

P-521 secret key for ES512.

§

Rsa(RsaPublicKey)

RSA public key for the RS* and PS* algorithms.

§

MlDsa44Public(MlDsa44PublicKey)

ML-DSA-44 public key for ML-DSA-44.

§

MlDsa44Secret(MlDsa44SecretKey)

ML-DSA-44 secret key for ML-DSA-44.

§

MlDsa65Public(MlDsa65PublicKey)

ML-DSA-65 public key for ML-DSA-65.

§

MlDsa65Secret(Box<MlDsa65SecretKey>)

ML-DSA-65 secret key for ML-DSA-65.

Boxed because an expanded ML-DSA-65 secret key is roughly 50 KiB, which would otherwise inflate every Key value and overflow the stack in debug builds.

§

MlDsa87Public(MlDsa87PublicKey)

ML-DSA-87 public key for ML-DSA-87.

§

MlDsa87Secret(Box<MlDsa87SecretKey>)

ML-DSA-87 secret key for ML-DSA-87.

Boxed because an expanded ML-DSA-87 secret key is roughly 82 KiB, which would otherwise inflate every Key value and overflow the stack in debug builds.

Implementations§

Source§

impl Key<'_>

Source

pub fn is_secret_key(&self) -> bool

Returns true if the Key is a secret key that can be used for signing.

Trait Implementations§

Source§

impl Debug for Key<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Signer for Key<'_>

Source§

fn sign(&self, message: &[u8]) -> Result<Signature, Error>

Source§

fn algorithm(&self) -> Algorithm

Source§

impl<'a> TryFrom<&'a Jwk> for Key<'a>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(jwk: &'a Jwk) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Verifier for Key<'_>

Source§

fn verify(&self, message: &[u8], signature: &[u8]) -> Result<(), Error>

Source§

fn algorithm(&self) -> Algorithm

Auto Trait Implementations§

§

impl<'a> Freeze for Key<'a>

§

impl<'a> RefUnwindSafe for Key<'a>

§

impl<'a> Send for Key<'a>

§

impl<'a> Sync for Key<'a>

§

impl<'a> Unpin for Key<'a>

§

impl<'a> UnsafeUnpin for Key<'a>

§

impl<'a> UnwindSafe for Key<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.