kaichao 10940321ff
Encode ratchet sate for serialization (#20)
* feat: costom encode for double ratchet

* chore: correct capacity

* chore: refactor reference

* chore: reader for parse bytes

* chore: extract reader

* chore: example with persist state.

* chore: update example

* chore: implement serde compatibility.

* chore: as_bytes

* chore: zerorize the secrec material

* chore: use as_types to return reference for static key.

* chore: extract example from basic demo
2026-01-29 09:19:52 +08:00

40 lines
1.0 KiB
Rust

use rand_core::OsRng;
use x25519_dalek::{PublicKey, StaticSecret};
use zeroize::{Zeroize, ZeroizeOnDrop};
use crate::types::SharedSecret;
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct InstallationKeyPair {
secret: StaticSecret,
public: PublicKey,
}
impl InstallationKeyPair {
pub fn generate() -> Self {
let secret = StaticSecret::random_from_rng(OsRng);
let public = PublicKey::from(&secret);
Self { secret, public }
}
pub fn dh(&self, their_public: &PublicKey) -> SharedSecret {
self.secret.diffie_hellman(their_public).to_bytes()
}
pub fn public(&self) -> &PublicKey {
&self.public
}
/// Export the secret key as raw bytes for serialization/storage.
pub fn secret_bytes(&self) -> &[u8; 32] {
self.secret.as_bytes()
}
/// Import the secret key from raw bytes.
pub fn from_secret_bytes(bytes: [u8; 32]) -> Self {
let secret = StaticSecret::from(bytes);
let public = PublicKey::from(&secret);
Self { secret, public }
}
}