entrust_core/backend/
age.rs1#[cfg(feature = "agent")]
2mod agent;
3pub mod identity;
4#[cfg(not(feature = "agent"))]
5mod no_agent;
6
7use std::io;
8use std::io::{Read, Write};
9use std::path::Path;
10use std::process::{Command, Stdio};
11
12use crate::age::identity::get_identity;
13use crate::backend::{exit_status_to_result, output_to_result};
14
15pub const RECIPIENT_FILE_NAME: &str = ".age-id";
16
17pub fn encrypt(content: &mut impl Read, recipient: &str, out_path: &Path) -> anyhow::Result<()> {
18 let (in_read, mut in_write) = io::pipe()?;
19 let mut child = Command::new("age")
20 .arg("--encrypt")
21 .arg("--armor")
22 .arg("--recipient")
23 .arg(recipient)
24 .arg("--output")
25 .arg(out_path.as_os_str())
26 .stdin(in_read)
27 .spawn()?;
28 io::copy(content, &mut in_write)?;
29 drop(in_write);
30 let exit_status = child.wait()?;
31 exit_status_to_result(exit_status, "age")
32}
33
34pub fn decrypt(path: &Path) -> anyhow::Result<String> {
35 let identity = get_identity()?;
36 let (in_read, mut in_write) = io::pipe()?;
37 let child = Command::new("age")
38 .arg("--decrypt")
39 .arg("--identity")
40 .arg("-")
41 .arg(path.as_os_str())
42 .stdin(in_read)
43 .stdout(Stdio::piped())
44 .stderr(Stdio::piped())
45 .spawn()?;
46 in_write.write_all(identity.as_slice())?;
47 drop(in_write);
48 let output = child.wait_with_output()?;
49 output_to_result(output)
50}