use crate::error::{Result, ToolErrorKind};
use log::{error, info};
use parsec_client::core::interface::operations::psa_algorithm::Algorithm;
use parsec_client::BasicClient;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
pub struct Encrypt {
#[structopt(short = "k", long = "key-name")]
key_name: String,
input_data: String,
}
impl Encrypt {
pub fn run(&self, basic_client: BasicClient) -> Result<()> {
let input = self.input_data.as_bytes();
let alg = basic_client
.key_attributes(&self.key_name)?
.policy
.permitted_algorithms;
let ciphertext = match alg {
Algorithm::AsymmetricEncryption(alg) => {
info!("Encrypting data with {:?}...", alg);
basic_client.psa_asymmetric_encrypt(&self.key_name, alg, input, None)?
}
Algorithm::Cipher(_) | Algorithm::Aead(_) => {
error!(
"Key's algorithm is {:?} which is not currently supported for encryption.",
alg
);
return Err(ToolErrorKind::NotSupported.into());
}
other => {
error!(
"Key's algorithm is {:?} which cannot be used for encryption.",
other
);
return Err(ToolErrorKind::WrongKeyAlgorithm.into());
}
};
let ciphertext = base64::encode(ciphertext);
println!("{}", ciphertext);
Ok(())
}
}