Skip to content

Instantly share code, notes, and snippets.

@goncalor
Last active May 3, 2025 13:05
Show Gist options
  • Save goncalor/01c92e4179669781e9a66f882c85fc92 to your computer and use it in GitHub Desktop.
Save goncalor/01c92e4179669781e9a66f882c85fc92 to your computer and use it in GitHub Desktop.
Sequoia symmetric encryption example
[package]
name = "sequoia-test"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "sequoia-test-password"
path = "password.rs"
[[bin]]
name = "sequoia-test-recipient"
path = "recipient.rs"
[dependencies]
sequoia-openpgp = "1.22"
//use openpgp::serialize::stream::padding::Padder;
use openpgp::serialize::stream::{Encryptor2, LiteralWriter, Message};
use sequoia_openpgp as openpgp;
use std::fs;
use std::io::Write;
fn main() {
let mut file = fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open("test.gpg")
.unwrap();
let message = Encryptor2::with_passwords(Message::new(&mut file), Some("password"))
.build()
.unwrap();
//let message = Padder::new(message).build().unwrap();
let mut w = LiteralWriter::new(message).build().unwrap();
w.write_all(b"").unwrap();
w.finalize().unwrap();
}
use openpgp::cert::prelude::*;
use openpgp::parse::Parse;
use openpgp::policy::StandardPolicy;
//use openpgp::serialize::stream::padding::Padder;
use openpgp::serialize::stream::{Encryptor2, LiteralWriter, Message};
use sequoia_openpgp as openpgp;
use std::fs;
use std::io::Write;
fn main() {
// mkdir gnupg-test-dir
// gpg --homedir gnupg-test-dir --generate-key
// gpg --homedir gnupg-test-dir --export --armour > pub.key
let cert = Cert::from_file("pub.key").unwrap();
//println!("{:?}", cert);
let p = &StandardPolicy::new();
let recipients = cert
.keys()
.with_policy(p, None)
.supported()
.alive()
.revoked(false)
.for_storage_encryption();
let mut file = fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open("test.gpg")
.unwrap();
let message = Message::new(&mut file);
let message = Encryptor2::for_recipients(message, recipients)
.build()
.unwrap();
//let message = Padder::new(message).build().unwrap();
let mut w = LiteralWriter::new(message).build().unwrap();
w.write_all(b"Hello world.").unwrap();
w.finalize().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment