Skip to content

Instantly share code, notes, and snippets.

@bahrom04
Last active November 25, 2025 05:46
Show Gist options
  • Select an option

  • Save bahrom04/56747f2f39b3760bbbba249f21638a64 to your computer and use it in GitHub Desktop.

Select an option

Save bahrom04/56747f2f39b3760bbbba249f21638a64 to your computer and use it in GitHub Desktop.
Vodozemac example in 2 accound chatting
[package]
name = "megolm"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
homepage = "#website#"
repository = "#repository#"
description = "#description#"
authors = ["bahrom04"]
# Dependencies that will included with final binary
[dependencies]
anyhow = "1.0.100"
vodozemac = "0.9.0"
# Development dependencies which aren't used in release binary
[dev-dependencies]
# Optimize release binary as much as possible
[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1
use anyhow::Result;
use vodozemac::olm::{Account, OlmMessage, SessionConfig};
fn main() -> Result<()> {
let alice = Account::new();
let mut bob = Account::new();
bob.generate_one_time_keys(1);
let bob_otk = *bob.one_time_keys().values().next().unwrap();
let mut alice_session = alice
.create_outbound_session(SessionConfig::version_2(), bob.curve25519_key(), bob_otk);
bob.mark_keys_as_published();
let message = "Keep it between us, OK?";
let alice_msg = alice_session.encrypt(message);
if let OlmMessage::PreKey(m) = alice_msg.clone() {
let result = bob.create_inbound_session(alice.curve25519_key(), &m)?;
let mut bob_session = result.session;
let what_bob_received = result.plaintext;
println!("bob: {:?}", what_bob_received);
assert_eq!(alice_session.session_id(), bob_session.session_id());
assert_eq!(message.as_bytes(), what_bob_received);
let bob_reply = "Yes. Take this, it's dangerous out there!";
let bob_encrypted_reply = bob_session.encrypt(bob_reply).into();
let what_alice_received = alice_session.decrypt(&bob_encrypted_reply)?;
assert_eq!(what_alice_received, bob_reply.as_bytes());
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment