Skip to main content

EcdsaKeyPair

Struct EcdsaKeyPair 

Source
pub struct EcdsaKeyPair { /* private fields */ }
Expand description

An ECDSA keypair fixture with various output formats.

Created via EcdsaFactoryExt::ecdsa(). Provides access to:

  • Private key in PKCS#8 PEM and DER formats
  • Public key in SPKI PEM and DER formats
  • Negative fixtures (corrupted PEM, truncated DER, mismatched keys)
  • JWK output (with the jwk feature)

§Examples

use uselesskey_core::Factory;
use uselesskey_ecdsa::{EcdsaFactoryExt, EcdsaSpec};

let fx = Factory::random();
let keypair = fx.ecdsa("my-service", EcdsaSpec::es256());

let private_pem = keypair.private_key_pkcs8_pem();
let public_der = keypair.public_key_spki_der();

assert!(private_pem.contains("BEGIN PRIVATE KEY"));
assert!(!public_der.is_empty());

Implementations§

Source§

impl EcdsaKeyPair

Source

pub fn spec(&self) -> EcdsaSpec

Returns the spec used to create this keypair.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
assert_eq!(kp.spec(), EcdsaSpec::es256());
Source

pub fn private_key_pkcs8_der(&self) -> &[u8]

PKCS#8 DER-encoded private key bytes.

§Examples
let fx = Factory::deterministic(Seed::from_env_value("test-seed").unwrap());
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let der = kp.private_key_pkcs8_der();
assert!(!der.is_empty());
Source

pub fn private_key_pkcs8_pem(&self) -> &str

PKCS#8 PEM-encoded private key.

§Examples
let fx = Factory::deterministic(Seed::from_env_value("test-seed").unwrap());
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let pem = kp.private_key_pkcs8_pem();
assert!(pem.starts_with("-----BEGIN PRIVATE KEY-----"));
Source

pub fn public_key_spki_der(&self) -> &[u8]

SPKI DER-encoded public key bytes.

§Examples
let fx = Factory::deterministic(Seed::from_env_value("test-seed").unwrap());
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let der = kp.public_key_spki_der();
assert!(!der.is_empty());
Source

pub fn public_key_spki_pem(&self) -> &str

SPKI PEM-encoded public key.

§Examples
let fx = Factory::deterministic(Seed::from_env_value("test-seed").unwrap());
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let pem = kp.public_key_spki_pem();
assert!(pem.starts_with("-----BEGIN PUBLIC KEY-----"));
Source

pub fn write_private_key_pkcs8_pem(&self) -> Result<TempArtifact, Error>

Write the PKCS#8 PEM private key to a tempfile and return the handle.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let temp = kp.write_private_key_pkcs8_pem().unwrap();
assert!(temp.path().exists());
Source

pub fn write_public_key_spki_pem(&self) -> Result<TempArtifact, Error>

Write the SPKI PEM public key to a tempfile and return the handle.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let temp = kp.write_public_key_spki_pem().unwrap();
assert!(temp.path().exists());
Source

pub fn private_key_pkcs8_pem_corrupt(&self, how: CorruptPem) -> String

Produce a corrupted variant of the PKCS#8 PEM.

§Examples
let fx = Factory::deterministic(Seed::from_env_value("test-seed").unwrap());
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let bad = kp.private_key_pkcs8_pem_corrupt(CorruptPem::BadHeader);
assert!(bad.contains("CORRUPTED"));
Source

pub fn private_key_pkcs8_pem_corrupt_deterministic( &self, variant: &str, ) -> String

Produce a deterministic corrupted PKCS#8 PEM using a variant string.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let bad = kp.private_key_pkcs8_pem_corrupt_deterministic("corrupt:v1");
assert!(!bad.is_empty());
Source

pub fn private_key_pkcs8_der_truncated(&self, len: usize) -> Vec<u8>

Produce a truncated variant of the PKCS#8 DER.

§Examples
let fx = Factory::deterministic(Seed::from_env_value("test-seed").unwrap());
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let truncated = kp.private_key_pkcs8_der_truncated(10);
assert_eq!(truncated.len(), 10);
Source

pub fn private_key_pkcs8_der_corrupt_deterministic( &self, variant: &str, ) -> Vec<u8>

Produce a deterministic corrupted PKCS#8 DER using a variant string.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let bad = kp.private_key_pkcs8_der_corrupt_deterministic("corrupt:v1");
assert!(!bad.is_empty());
Source

pub fn mismatched_public_key_spki_der(&self) -> Vec<u8>

Return a valid (parseable) public key that does not match this private key.

§Examples
let fx = Factory::deterministic(Seed::from_env_value("test-seed").unwrap());
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let wrong_pub = kp.mismatched_public_key_spki_der();
assert_ne!(wrong_pub, kp.public_key_spki_der());
Source

pub fn kid(&self) -> String

A stable key identifier derived from the public key (base64url blake3 hash prefix).

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let kid = kp.kid();
assert!(!kid.is_empty());
Source

pub fn public_key_jwk(&self) -> PublicJwk

Alias for Self::public_jwk.

Requires the jwk feature.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let jwk = kp.public_key_jwk();
assert_eq!(jwk.to_value()["kty"], "EC");
Source

pub fn public_jwk(&self) -> PublicJwk

Public JWK for this keypair (kty=EC, crv=P-256 or P-384, alg=ES256 or ES384).

Requires the jwk feature.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let jwk = kp.public_jwk();
let val = jwk.to_value();
assert_eq!(val["kty"], "EC");
assert_eq!(val["crv"], "P-256");
Source

pub fn private_key_jwk(&self) -> PrivateJwk

Private JWK for this keypair (kty=EC, crv=…, alg=…, d=…).

Requires the jwk feature.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let jwk = kp.private_key_jwk();
let val = jwk.to_value();
assert_eq!(val["kty"], "EC");
assert!(val["d"].is_string());
Source

pub fn public_jwks(&self) -> Jwks

JWKS containing a single public key.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let jwks = kp.public_jwks();
assert!(jwks.to_value()["keys"].is_array());
Source

pub fn public_jwk_json(&self) -> Value

Public JWK serialized to serde_json::Value.

Requires the jwk feature.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let val = kp.public_jwk_json();
assert_eq!(val["kty"], "EC");
Source

pub fn public_jwks_json(&self) -> Value

JWKS serialized to serde_json::Value.

Requires the jwk feature.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let val = kp.public_jwks_json();
assert!(val["keys"].is_array());
Source

pub fn private_key_jwk_json(&self) -> Value

Private JWK serialized to serde_json::Value.

Requires the jwk feature.

§Examples
let fx = Factory::random();
let kp = fx.ecdsa("svc", EcdsaSpec::es256());
let val = kp.private_key_jwk_json();
assert_eq!(val["kty"], "EC");
assert!(val["d"].is_string());

Trait Implementations§

Source§

impl Clone for EcdsaKeyPair

Source§

fn clone(&self) -> EcdsaKeyPair

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EcdsaKeyPair

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V