entrust_core/backend/
gpg.rs

1use crate::backend::{exit_status_to_result, output_to_result};
2use std::io;
3use std::io::Read;
4use std::path::Path;
5use std::process::{Command, Stdio};
6
7pub const RECIPIENT_FILE_NAME: &str = ".gpg-id";
8
9pub fn encrypt(content: &mut impl Read, recipient: &str, out_path: &Path) -> anyhow::Result<()> {
10    let (in_read, mut in_write) = io::pipe()?;
11    let child = Command::new("gpg")
12        .arg("--encrypt")
13        .arg("--armor")
14        .arg("--quiet")
15        .arg("--recipient")
16        .arg(recipient)
17        .arg("--output")
18        .arg(out_path.as_os_str())
19        .stdin(in_read)
20        .spawn()?;
21    io::copy(content, &mut in_write)?;
22    drop(in_write);
23    let exit_status = child.wait_with_output()?.status;
24    exit_status_to_result(exit_status, "gpg")
25}
26
27pub fn decrypt(path: &Path) -> anyhow::Result<String> {
28    let output = Command::new("gpg")
29        .arg("--decrypt")
30        .arg("--quiet")
31        .arg(path.as_os_str())
32        .stdin(Stdio::inherit())
33        .output()?;
34    output_to_result(output)
35}