feat: updates

This commit is contained in:
2023-09-07 08:38:26 +08:00
parent 8939237d99
commit 1230601ed2
5 changed files with 40 additions and 27 deletions

View File

@@ -2,10 +2,10 @@ 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";
// 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)]
@@ -37,20 +37,38 @@ pub struct TinyEncryptMeta {
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TinyEncryptEnvelop {
pub r#type: String,
pub r#type: TinyEncryptEnvelopType,
pub kid: String,
pub desc: Option<String>,
pub encrypted_key: String,
}
// use serde...
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum TinyEncryptEnvelopType {
#[serde(rename = "pgp")]
Pgp,
#[serde(rename = "age")]
Age,
#[serde(rename = "ecdh")]
Ecdh,
#[serde(rename = "kms")]
Kms,
}
impl TinyEncryptEnvelopType {
pub fn get_upper_name(&self) -> String {
self.get_name().to_uppercase()
}
pub fn get_name(&self) -> &'static str {
match self {
TinyEncryptEnvelopType::Pgp => "pgp",
TinyEncryptEnvelopType::Age => "age",
TinyEncryptEnvelopType::Ecdh => "ecdh",
TinyEncryptEnvelopType::Kms => "kms",
}
}
}
impl TinyEncryptMeta {
pub fn normalize(&mut self) {
if self.envelops.is_none() {
@@ -65,7 +83,7 @@ impl TinyEncryptMeta {
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(),
r#type: TinyEncryptEnvelopType::Kms,
kid: "".into(),
desc: None,
encrypted_key: envelop.into(),
@@ -78,7 +96,7 @@ impl TinyEncryptMeta {
if let (Some(pgp_envelop), Some(pgp_fingerprint), Some(envelops))
= (&self.pgp_envelop, &self.pgp_fingerprint, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: ENVELOP_TYPE_PGP.into(),
r#type: TinyEncryptEnvelopType::Pgp,
kid: pgp_fingerprint.into(),
desc: None,
encrypted_key: pgp_envelop.into(),
@@ -92,7 +110,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: ENVELOP_TYPE_AGE.into(),
r#type: TinyEncryptEnvelopType::Age,
kid: age_recipient.into(),
desc: None,
encrypted_key: age_envelop.into(),
@@ -106,7 +124,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: ENVELOP_TYPE_ECDH.into(),
r#type: TinyEncryptEnvelopType::Ecdh,
kid: ecdh_point.into(),
desc: None,
encrypted_key: ecdh_envelop.into(),