Skip to main content

KeyPair

Enum KeyPair 

Source
pub enum KeyPair {
    Ed25519(Ed25519Signer),
}
Expand description

Generic key pair wrapper that can hold any DSA implementation

This provides algorithm-agnostic operations for signing and verification.

Cloning a KeyPair is cheap because the underlying secret keys are stored in Arc, so only the reference is cloned, not the encrypted data.

§Example

use ave_identity::keys::{KeyPair, KeyPairAlgorithm, DSA};

// Generate a key pair
let keypair = KeyPair::generate(KeyPairAlgorithm::Ed25519).expect("Failed to generate key pair");

let message = b"Hello, World!";

// Sign message using generic interface
let signature = keypair.sign(message).unwrap();

// Get public key
let public_key = keypair.public_key();

// Verify
assert!(public_key.verify(message, &signature).is_ok());

Variants§

Implementations§

Source§

impl KeyPair

Source

pub fn generate(key_type: KeyPairAlgorithm) -> Result<Self, CryptoError>

Generate a new random key pair of the specified type

Source

pub fn from_secret_der(der: &[u8]) -> Result<Self, CryptoError>

Create key pair from PKCS#8 DER-encoded secret key

This method automatically detects the algorithm from the OID in the DER structure. Supported OIDs:

  • Ed25519: 1.3.101.112
§Errors
  • Returns InvalidDerFormat if the DER structure is malformed
  • Returns UnsupportedAlgorithm if the algorithm OID is not supported
  • Returns InvalidSecretKey if the key data is invalid
§Example
use ave_identity::keys::KeyPair;

let der_bytes = std::fs::read("private_key.der").unwrap();
let keypair = KeyPair::from_secret_der(&der_bytes).unwrap();
Source

pub fn from_seed( key_type: KeyPairAlgorithm, seed: &[u8; 32], ) -> Result<Self, CryptoError>

Create key pair from seed

Source

pub fn derive_from_data( key_type: KeyPairAlgorithm, data: &[u8], ) -> Result<Self, CryptoError>

Derive key pair from arbitrary data (will be hashed)

Source

pub fn from_secret_key(secret_key: &[u8]) -> Result<Self, CryptoError>

Create key pair from secret key bytes

Attempts to auto-detect the algorithm from key length. For explicit algorithm selection, use from_secret_key_with_type.

Source

pub fn from_secret_key_with_type( key_type: KeyPairAlgorithm, secret_key: &[u8], ) -> Result<Self, CryptoError>

Create key pair from secret key bytes with explicit type

Source

pub fn key_type(&self) -> KeyPairAlgorithm

Get the key pair type

Source

pub fn sign(&self, message: &[u8]) -> Result<SignatureIdentifier, CryptoError>

Sign a message using the appropriate algorithm

Source

pub fn algorithm(&self) -> DSAlgorithm

Get the algorithm used by this key pair

Source

pub fn algorithm_id(&self) -> u8

Get the algorithm identifier

Source

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

Get the public key bytes

Source

pub fn public_key(&self) -> PublicKey

Get the public key as a PublicKey wrapper

Source

pub fn secret_key_bytes(&self) -> Result<Vec<u8>, CryptoError>

Get the secret key bytes (if available)

Source

pub fn to_bytes(&self) -> Result<Vec<u8>, CryptoError>

Serialize to bytes (includes algorithm identifier and secret key)

§Warning

This exposes the secret key. Use with extreme caution.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, CryptoError>

Deserialize from bytes (includes algorithm identifier)

Source

pub fn to_secret_der(&self) -> Result<Vec<u8>, CryptoError>

Serialize to PKCS#8 DER format

This creates a DER-encoded PKCS#8 PrivateKeyInfo structure containing the secret key and algorithm identifier.

§Errors
  • Returns InvalidSecretKey if the secret key cannot be retrieved
§Example
use ave_identity::keys::{KeyPair, KeyPairAlgorithm};

let keypair = KeyPair::generate(KeyPairAlgorithm::Ed25519).unwrap();
let der_bytes = keypair.to_secret_der().unwrap();
std::fs::write("private_key.der", der_bytes).unwrap();

Trait Implementations§

Source§

impl Clone for KeyPair

Source§

fn clone(&self) -> KeyPair

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 DSA for KeyPair

Source§

fn algorithm_id(&self) -> u8

Get the algorithm identifier (1 byte)
Source§

fn signature_length(&self) -> usize

Get the expected signature length in bytes (excluding identifier)
Source§

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

Sign the message
Source§

fn algorithm(&self) -> DSAlgorithm

Get the algorithm enum variant
Source§

fn public_key_bytes(&self) -> Vec<u8>

Get the public key bytes
Source§

impl Debug for KeyPair

Source§

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

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

impl Default for KeyPair

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for KeyPair

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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