feat: update tiny-encrypt info

This commit is contained in:
2023-09-06 01:13:15 +08:00
parent d96ebdd5ba
commit 34fd7d0e47
4 changed files with 44 additions and 26 deletions

View File

@@ -2,6 +2,11 @@ use serde::{Deserialize, Serialize};
pub const TINY_ENCRYPT_VERSION: &'static str = "1.0";
pub const ENVELOP_TYPE_KMS: &'static str = "kms";
pub const ENVELOP_TYPE_PGP: &'static str = "pgp";
pub const ENVELOP_TYPE_AGE: &'static str = "age";
pub const ENVELOP_TYPE_ECDH: &'static str = "ecdh";
/// Specification: [Tiny Encrypt Spec V1.1](https://git.hatter.ink/hatter/tiny-encrypt-java/src/branch/master/TinyEncryptSpecV1.1.md)
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -51,17 +56,29 @@ impl TinyEncryptMeta {
if self.envelops.is_none() {
self.envelops = Some(vec![]);
}
self.normalize_ppg_envelop();
self.normalize_envelop();
self.normalize_pgp_envelop();
self.normalize_age_envelop();
self.normalize_ecdh_envelop();
self.normalize_envelop();
}
fn normalize_ppg_envelop(&mut self) {
fn normalize_envelop(&mut self) {
if let (Some(envelop), Some(envelops)) = (&self.envelop, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: ENVELOP_TYPE_KMS.into(),
kid: "".into(),
desc: None,
encrypted_key: envelop.into(),
});
self.envelop = None;
}
}
fn normalize_pgp_envelop(&mut self) {
if let (Some(pgp_envelop), Some(pgp_fingerprint), Some(envelops))
= (&self.pgp_envelop, &self.pgp_fingerprint, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: "pgp".to_string(),
r#type: ENVELOP_TYPE_PGP.into(),
kid: pgp_fingerprint.into(),
desc: None,
encrypted_key: pgp_envelop.into(),
@@ -75,7 +92,7 @@ impl TinyEncryptMeta {
if let (Some(age_envelop), Some(age_recipient), Some(envelops))
= (&self.age_envelop, &self.age_recipient, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: "age".to_string(),
r#type: ENVELOP_TYPE_AGE.into(),
kid: age_recipient.into(),
desc: None,
encrypted_key: age_envelop.into(),
@@ -89,7 +106,7 @@ impl TinyEncryptMeta {
if let (Some(ecdh_envelop), Some(ecdh_point), Some(envelops))
= (&self.ecdh_envelop, &self.ecdh_point, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: "ecdh".to_string(),
r#type: ENVELOP_TYPE_ECDH.into(),
kid: ecdh_point.into(),
desc: None,
encrypted_key: ecdh_envelop.into(),
@@ -98,16 +115,4 @@ impl TinyEncryptMeta {
self.ecdh_point = None;
}
}
fn normalize_envelop(&mut self) {
if let (Some(envelop), Some(envelops)) = (&self.envelop, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: "kms".to_string(),
kid: "".into(),
desc: None,
encrypted_key: envelop.into(),
});
self.envelop = None;
}
}
}