pub struct Ed25519KeyPair {
signing_key: SigningKey,
}Expand description
Ed25519 keypair for signing and verification.
Fields§
§signing_key: SigningKeyImplementations§
Source§impl Ed25519KeyPair
impl Ed25519KeyPair
Sourcepub fn generate() -> CrabResult<Self>
pub fn generate() -> CrabResult<Self>
Generates a new random Ed25519 keypair.
§Example
use crabgraph::asym::Ed25519KeyPair;
let keypair = Ed25519KeyPair::generate().unwrap();Sourcepub fn from_secret_bytes(secret: &[u8]) -> CrabResult<Self>
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.
Sourcepub fn secret_bytes(&self) -> &[u8; 32]
pub fn secret_bytes(&self) -> &[u8; 32]
Sourcepub fn public_key(&self) -> Ed25519PublicKey
pub fn public_key(&self) -> Ed25519PublicKey
Returns the public key.
Sourcepub fn sign(&self, message: &[u8]) -> Ed25519Signature
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);Sourcepub fn sign_with_verification(
&self,
message: &[u8],
) -> CrabResult<Ed25519Signature>
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.
Sourcepub fn verify(
&self,
message: &[u8],
signature: &Ed25519Signature,
) -> CrabResult<bool>
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());Sourcepub fn to_pkcs8_der(&self) -> CrabResult<Vec<u8>>
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();Sourcepub fn from_pkcs8_der(der: &[u8]) -> CrabResult<Self>
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());Sourcepub fn to_pkcs8_pem(&self) -> CrabResult<String>
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-----"));Sourcepub fn from_pkcs8_pem(pem: &str) -> CrabResult<Self>
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§
impl Freeze for Ed25519KeyPair
impl RefUnwindSafe for Ed25519KeyPair
impl Send for Ed25519KeyPair
impl Sync for Ed25519KeyPair
impl Unpin for Ed25519KeyPair
impl UnwindSafe for Ed25519KeyPair
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more