feat: v1.7.0, support GPG encrypt and decrypt

This commit is contained in:
2023-12-27 00:09:55 +08:00
parent e60f491801
commit 004df06df2
7 changed files with 45 additions and 12 deletions

View File

@@ -41,6 +41,8 @@ use std::process::{Command, Stdio};
use rust_util::{opt_result, opt_value_result, simple_error, XResult};
use crate::util_env;
pub fn gpg_encrypt(key_id: &str, message: &[u8]) -> XResult<String> {
let message_hex = hex::encode(message);
let echo = opt_result!(Command::new("echo").arg(&message_hex)
@@ -48,7 +50,7 @@ pub fn gpg_encrypt(key_id: &str, message: &[u8]) -> XResult<String> {
.spawn(), "echo message failed: {}");
let echo_stdout = opt_value_result!(echo.stdout, "Get echo stdout failed: none");
let mut cmd = Command::new("gpg");
let mut cmd = Command::new(&get_gpg_cmd());
let encrypt_result = cmd
.args([
"-e", "-a", "--no-comment",
@@ -62,7 +64,7 @@ pub fn gpg_encrypt(key_id: &str, message: &[u8]) -> XResult<String> {
let stderr = String::from_utf8_lossy(&encrypt_output.stderr).to_string();
if !encrypt_output.status.success() {
return simple_error!(
"GPG encrypt failed: {:?}, stdout: {}, stderr: {}",
"GPG encrypt failed: {:?}\n- stdout: {}\n- stderr: {}",
encrypt_output.status.code(), stdout, stderr
);
}
@@ -75,7 +77,7 @@ pub fn gpg_decrypt(message: &str) -> XResult<Vec<u8>> {
.spawn(), "echo message failed: {}");
let echo_stdout = opt_value_result!(echo.stdout, "Get echo stdout failed: none");
let mut cmd = Command::new("gpg");
let mut cmd = Command::new(&get_gpg_cmd());
let encrypt_result = cmd
.arg("-d")
.stdin(Stdio::from(echo_stdout))
@@ -85,10 +87,14 @@ pub fn gpg_decrypt(message: &str) -> XResult<Vec<u8>> {
let stderr = String::from_utf8_lossy(&decrypt_output.stderr).to_string();
if !decrypt_output.status.success() {
return simple_error!(
"GPG decrypt failed: {:?}, stdout: {}, stderr: {}",
"GPG decrypt failed: {:?}\n- stdout: {}\n- stderr: {}",
decrypt_output.status.code(), stdout, stderr
);
}
let decrypted = opt_result!(hex::decode(&stdout.trim()), "Decode decrypted message failed: {}");
Ok(decrypted)
}
fn get_gpg_cmd() -> String {
util_env::get_gpg_cmd().unwrap_or("gpg".to_string())
}