feat: pgp rsa public key encrypt
This commit is contained in:
@@ -3,11 +3,13 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
use rsa::Pkcs1v15Encrypt;
|
||||||
use rust_util::{debugging, failure, opt_result, simple_error, success, XResult};
|
use rust_util::{debugging, failure, opt_result, simple_error, success, XResult};
|
||||||
|
|
||||||
use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop};
|
use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop};
|
||||||
|
use crate::crypto_rsa::parse_spki;
|
||||||
use crate::spec::{EncMetadata, TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta};
|
use crate::spec::{EncMetadata, TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta};
|
||||||
use crate::util::TINY_ENC_CONFIG_FILE;
|
use crate::util::{encode_base64, TINY_ENC_CONFIG_FILE};
|
||||||
|
|
||||||
#[derive(Debug, Args)]
|
#[derive(Debug, Args)]
|
||||||
pub struct CmdEncrypt {
|
pub struct CmdEncrypt {
|
||||||
@@ -81,11 +83,14 @@ fn encrypt_envelop_ecdh(key: &[u8], envelop: &TinyEncryptConfigEnvelop) -> XResu
|
|||||||
|
|
||||||
|
|
||||||
fn encrypt_envelop_pgp(key: &[u8], envelop: &TinyEncryptConfigEnvelop) -> XResult<TinyEncryptEnvelop> {
|
fn encrypt_envelop_pgp(key: &[u8], envelop: &TinyEncryptConfigEnvelop) -> XResult<TinyEncryptEnvelop> {
|
||||||
|
let pgp_public_key = opt_result!(parse_spki(&envelop.public_part), "Parse PGP public key failed: {}");
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let encrypted_key = opt_result!(pgp_public_key.encrypt(&mut rng, Pkcs1v15Encrypt, key), "PGP public key encrypt failed: {}");
|
||||||
Ok(TinyEncryptEnvelop {
|
Ok(TinyEncryptEnvelop {
|
||||||
r#type: envelop.r#type,
|
r#type: envelop.r#type,
|
||||||
kid: envelop.kid.clone(),
|
kid: envelop.kid.clone(),
|
||||||
desc: envelop.desc.clone(),
|
desc: envelop.desc.clone(),
|
||||||
encrypted_key: "".to_string(), // TODO ...
|
encrypted_key: encode_base64(&encrypted_key),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user