Ed25519KeyPair

Struct Ed25519KeyPair 

Source
pub struct Ed25519KeyPair {
    signing_key: SigningKey,
}
Expand description

Ed25519 keypair for signing and verification.

Fields§

§signing_key: SigningKey

Implementations§

Source§

impl Ed25519KeyPair

Source

pub fn generate() -> CrabResult<Self>

Generates a new random Ed25519 keypair.

§Example
use crabgraph::asym::Ed25519KeyPair;

let keypair = Ed25519KeyPair::generate().unwrap();
Source

pub fn from_secret_bytes(secret: &[u8]) -> CrabResult<Self>

Creates a keypair from a 32-byte secret key.

§Security Warning

The secret key must be kept confidential and zeroized after use.

Source

pub fn secret_bytes(&self) -> &[u8; 32]

Returns the secret key bytes.

§Security Warning

Handle with care! Zeroize after use.

Source

pub fn public_key(&self) -> Ed25519PublicKey

Returns the public key.

Source

pub fn sign(&self, message: &[u8]) -> Ed25519Signature

Signs a message and returns the signature.

§Example
use crabgraph::asym::Ed25519KeyPair;

let keypair = Ed25519KeyPair::generate().unwrap();
let message = b"Important message";
let signature = keypair.sign(message);
Source

pub fn sign_with_verification( &self, message: &[u8], ) -> CrabResult<Ed25519Signature>

Signs a message and verifies it’s correct (paranoid mode).

This performs an extra verification step to ensure the signature is valid.

Source

pub fn verify( &self, message: &[u8], signature: &Ed25519Signature, ) -> CrabResult<bool>

Verifies a signature on a message using this keypair’s public key.

§Example
use crabgraph::asym::Ed25519KeyPair;

let keypair = Ed25519KeyPair::generate().unwrap();
let message = b"Important message";
let signature = keypair.sign(message);

assert!(keypair.verify(message, &signature).unwrap());
assert!(!keypair.verify(b"Wrong message", &signature).unwrap());
Source

pub fn to_pkcs8_der(&self) -> CrabResult<Vec<u8>>

Exports the keypair to PKCS#8 DER format.

This is the binary encoding format for private keys.

§Example
use crabgraph::asym::Ed25519KeyPair;

let keypair = Ed25519KeyPair::generate().unwrap();
let der = keypair.to_pkcs8_der().unwrap();
let restored = Ed25519KeyPair::from_pkcs8_der(&der).unwrap();
Source

pub fn from_pkcs8_der(der: &[u8]) -> CrabResult<Self>

Imports a keypair from PKCS#8 DER format.

§Example
use crabgraph::asym::Ed25519KeyPair;

let keypair = Ed25519KeyPair::generate().unwrap();
let der = keypair.to_pkcs8_der().unwrap();
let restored = Ed25519KeyPair::from_pkcs8_der(&der).unwrap();

let message = b"Test";
let sig = keypair.sign(message);
assert!(restored.verify(message, &sig).unwrap());
Source

pub fn to_pkcs8_pem(&self) -> CrabResult<String>

Exports the keypair to PKCS#8 PEM format.

This is the text-based encoding format commonly used in configuration files.

§Example
use crabgraph::asym::Ed25519KeyPair;

let keypair = Ed25519KeyPair::generate().unwrap();
let pem = keypair.to_pkcs8_pem().unwrap();
assert!(pem.starts_with("-----BEGIN PRIVATE KEY-----"));
Source

pub fn from_pkcs8_pem(pem: &str) -> CrabResult<Self>

Imports a keypair from PKCS#8 PEM format.

§Example
use crabgraph::asym::Ed25519KeyPair;

let keypair = Ed25519KeyPair::generate().unwrap();
let pem = keypair.to_pkcs8_pem().unwrap();
let restored = Ed25519KeyPair::from_pkcs8_pem(&pem).unwrap();

let message = b"Test";
let sig = keypair.sign(message);
assert!(restored.verify(message, &sig).unwrap());

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

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

§

fn vzip(self) -> V