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
jwkfeature)
§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
impl EcdsaKeyPair
Sourcepub fn spec(&self) -> EcdsaSpec
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());Sourcepub fn private_key_pkcs8_der(&self) -> &[u8] ⓘ
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());Sourcepub fn private_key_pkcs8_pem(&self) -> &str
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-----"));Sourcepub fn public_key_spki_der(&self) -> &[u8] ⓘ
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());Sourcepub fn public_key_spki_pem(&self) -> &str
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-----"));Sourcepub fn write_private_key_pkcs8_pem(&self) -> Result<TempArtifact, Error>
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());Sourcepub fn write_public_key_spki_pem(&self) -> Result<TempArtifact, Error>
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());Sourcepub fn private_key_pkcs8_pem_corrupt(&self, how: CorruptPem) -> String
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"));Sourcepub fn private_key_pkcs8_pem_corrupt_deterministic(
&self,
variant: &str,
) -> String
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());Sourcepub fn private_key_pkcs8_der_truncated(&self, len: usize) -> Vec<u8> ⓘ
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);Sourcepub fn private_key_pkcs8_der_corrupt_deterministic(
&self,
variant: &str,
) -> Vec<u8> ⓘ
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());Sourcepub fn mismatched_public_key_spki_der(&self) -> Vec<u8> ⓘ
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());Sourcepub fn kid(&self) -> String
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());Sourcepub fn public_key_jwk(&self) -> PublicJwk
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");Sourcepub fn public_jwk(&self) -> PublicJwk
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");Sourcepub fn private_key_jwk(&self) -> PrivateJwk
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());Sourcepub fn public_jwks(&self) -> Jwks
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());Sourcepub fn public_jwk_json(&self) -> Value
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");Sourcepub fn public_jwks_json(&self) -> Value
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());Sourcepub fn private_key_jwk_json(&self) -> Value
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
impl Clone for EcdsaKeyPair
Source§fn clone(&self) -> EcdsaKeyPair
fn clone(&self) -> EcdsaKeyPair
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more