use crate::core::{helpers, parsers};
use crate::handler::types::{DecryptOutput, EncryptOutput, LibrageResponse};
pub fn encrypt_with_ssh(plaintext: &[u8], ssh_public_key: &str) -> LibrageResponse<EncryptOutput> {
let recipient = match parsers::parse_ssh_recipient(ssh_public_key) {
Ok(r) => r,
Err(e) => return LibrageResponse::err(e.into()),
};
let mut ciphertext = Vec::new();
match helpers::encrypt_to_writer(plaintext, &[recipient], &mut ciphertext) {
Ok(_) => LibrageResponse::ok(EncryptOutput::new(ciphertext, false)),
Err(e) => LibrageResponse::err(e.into()),
}
}
pub fn decrypt_with_ssh(
ciphertext: &[u8],
ssh_private_key: &str,
passphrase: Option<&str>,
) -> LibrageResponse<DecryptOutput> {
let identity = match parsers::parse_ssh_identity(ssh_private_key, passphrase) {
Ok(id) => id,
Err(e) => return LibrageResponse::err(e.into()),
};
match helpers::decrypt_to_vec(ciphertext, &[identity]) {
Ok(plaintext) => LibrageResponse::ok(DecryptOutput::new(plaintext)),
Err(e) => LibrageResponse::err(e.into()),
}
}