// Copyright (c) 2026 The Sojourn contributors // SPDX-License-Identifier: MIT //! Command-line demo of the Sojourn envelope, with a folder standing in for a mailbox. //! Not a messenger: no Tor, no network, no contacts. See README.md. use std::fs; use std::path::{Path, PathBuf}; use std::process::exit; use sojourn::{from_hex, open, seal, to_hex, Identity, PublicIdentity}; const USAGE: &str = "sojourn (skeleton): sealed, padded, post-quantum hybrid envelopes sojourn keygen write .secret and .pub sojourn send .secret .pub sojourn fetch .secret print and remove envelopes meant for you The mailbox is a plain folder of opaque files. Nothing in it says who sent what to whom. This is a protocol skeleton for review, not a tool to protect anyone."; fn die(msg: &str) -> ! { eprintln!("{msg}"); exit(1) } fn read_hex(path: &Path) -> Vec { let s = fs::read_to_string(path).unwrap_or_else(|_| die(&format!("can't read {}", path.display()))); from_hex(&s).unwrap_or_else(|| die(&format!("{} isn't a key file", path.display()))) } fn random_name() -> String { let mut b = [0u8; 16]; getrandom::getrandom(&mut b).unwrap_or_else(|_| die("no randomness")); to_hex(&b) } fn main() { let args: Vec = std::env::args().skip(1).collect(); match args.iter().map(String::as_str).collect::>().as_slice() { ["keygen", name] => { let id = Identity::generate().unwrap_or_else(|_| die("no randomness")); let secret = PathBuf::from(format!("{name}.secret")); if secret.exists() { die(&format!("{} already exists", secret.display())); } fs::write(&secret, to_hex(&id.to_bytes())).unwrap_or_else(|_| die("can't write secret")); #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; let _ = fs::set_permissions(&secret, fs::Permissions::from_mode(0o600)); } fs::write(format!("{name}.pub"), to_hex(&id.public().to_bytes())).unwrap_or_else(|_| die("can't write pub")); println!("wrote {name}.secret (keep it) and {name}.pub (share it)"); } ["send", from, to, mailbox, message] => { let me = Identity::from_bytes(&read_hex(Path::new(from))).unwrap_or_else(|_| die("bad secret key")); let them = PublicIdentity::from_bytes(&read_hex(Path::new(to))).unwrap_or_else(|_| die("bad public key")); let env = seal(&me, &them, message.as_bytes()).unwrap_or_else(|e| die(&format!("can't seal: {e:?}"))); fs::create_dir_all(mailbox).unwrap_or_else(|_| die("can't open mailbox")); let path = Path::new(mailbox).join(random_name()); fs::write(&path, &env).unwrap_or_else(|_| die("can't write envelope")); println!("dropped a {}-byte envelope in {mailbox}", env.len()); } ["fetch", me, mailbox] => { let me = Identity::from_bytes(&read_hex(Path::new(me))).unwrap_or_else(|_| die("bad secret key")); let entries = fs::read_dir(mailbox).unwrap_or_else(|_| die("can't open mailbox")); let mut n = 0; for entry in entries.flatten() { let Ok(env) = fs::read(entry.path()) else { continue }; if let Ok(got) = open(&me, &env) { n += 1; println!("from {}: {}", &to_hex(&got.from_ed)[..16], String::from_utf8_lossy(&got.message)); let _ = fs::remove_file(entry.path()); } } if n == 0 { println!("nothing for you"); } } _ => { println!("{USAGE}"); exit(if args.is_empty() { 0 } else { 2 }); } } }