pub struct DeviceKeypair { /* private fields */ }Expand description
Ed25519 keypair for device identity and authentication.
The keypair consists of:
- A 32-byte secret (signing) key
- A 32-byte public (verifying) key
The DeviceId is derived from the public key.
§Example
use peat_mesh::security::DeviceKeypair;
// Generate a new keypair
let keypair = DeviceKeypair::generate();
// Get the device ID
let device_id = keypair.device_id();
// Sign a message
let message = b"hello world";
let signature = keypair.sign(message);
// Verify the signature
assert!(keypair.verify(message, &signature).is_ok());Implementations§
Source§impl DeviceKeypair
impl DeviceKeypair
Sourcepub fn generate() -> DeviceKeypair
pub fn generate() -> DeviceKeypair
Generate a new random keypair.
Sourcepub fn from_signing_key(signing_key: SigningKey) -> DeviceKeypair
pub fn from_signing_key(signing_key: SigningKey) -> DeviceKeypair
Create from an existing signing key.
Sourcepub fn from_seed(
seed: &[u8],
context: &str,
) -> Result<DeviceKeypair, SecurityError>
pub fn from_seed( seed: &[u8], context: &str, ) -> Result<DeviceKeypair, SecurityError>
Create a deterministic keypair from a seed and context string.
Uses HKDF-SHA256 to derive 32 bytes from seed (IKM) with
context as the info parameter. Same seed + context always
produces the same keypair; different context → different key.
Useful for Kubernetes deployments where pods derive stable identities from a shared secret + pod-specific context.
Sourcepub fn from_secret_bytes(bytes: &[u8]) -> Result<DeviceKeypair, SecurityError>
pub fn from_secret_bytes(bytes: &[u8]) -> Result<DeviceKeypair, SecurityError>
Create from raw secret key bytes (32 bytes).
Sourcepub fn load_from_file(path: &Path) -> Result<DeviceKeypair, SecurityError>
pub fn load_from_file(path: &Path) -> Result<DeviceKeypair, SecurityError>
Load keypair from a file (raw 32-byte secret key).
Sourcepub fn save_to_file(&self, path: &Path) -> Result<(), SecurityError>
pub fn save_to_file(&self, path: &Path) -> Result<(), SecurityError>
Save keypair to a file (raw 32-byte secret key).
§Security Note
In MVP, this saves the key unencrypted. Production deployments should use encrypted key storage (Phase 2).
Sourcepub fn verifying_key(&self) -> VerifyingKey
pub fn verifying_key(&self) -> VerifyingKey
Get the public (verifying) key.
Sourcepub fn public_key_bytes(&self) -> [u8; 32]
pub fn public_key_bytes(&self) -> [u8; 32]
Get the public key as bytes.
Sourcepub fn secret_key_bytes(&self) -> [u8; 32]
pub fn secret_key_bytes(&self) -> [u8; 32]
Get the secret key bytes (32 bytes).
§Security Warning
This exposes the private key material. Only use for:
- Secure storage/persistence
- Cross-crate interop (e.g., converting to peat_btle::DeviceIdentity)
Sourcepub fn verify(
&self,
message: &[u8],
signature: &Signature,
) -> Result<(), SecurityError>
pub fn verify( &self, message: &[u8], signature: &Signature, ) -> Result<(), SecurityError>
Verify a signature against this keypair’s public key.
Sourcepub fn verify_with_key(
public_key: &VerifyingKey,
message: &[u8],
signature: &Signature,
) -> Result<(), SecurityError>
pub fn verify_with_key( public_key: &VerifyingKey, message: &[u8], signature: &Signature, ) -> Result<(), SecurityError>
Verify a signature against a specific public key.
Sourcepub fn signature_from_bytes(bytes: &[u8]) -> Result<Signature, SecurityError>
pub fn signature_from_bytes(bytes: &[u8]) -> Result<Signature, SecurityError>
Parse a signature from bytes.
Sourcepub fn verifying_key_from_bytes(
bytes: &[u8],
) -> Result<VerifyingKey, SecurityError>
pub fn verifying_key_from_bytes( bytes: &[u8], ) -> Result<VerifyingKey, SecurityError>
Parse a verifying key from bytes.
Trait Implementations§
Source§impl Clone for DeviceKeypair
impl Clone for DeviceKeypair
Source§fn clone(&self) -> DeviceKeypair
fn clone(&self) -> DeviceKeypair
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for DeviceKeypair
impl RefUnwindSafe for DeviceKeypair
impl Send for DeviceKeypair
impl Sync for DeviceKeypair
impl Unpin for DeviceKeypair
impl UnsafeUnpin for DeviceKeypair
impl UnwindSafe for DeviceKeypair
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more