1use crate::{Keypair, Signature};
2use base64::{engine::general_purpose, Engine as _};
3use rand::{distr::Alphanumeric, Rng};
4use serde::{Deserialize, Serialize};
5
6pub const NCRYPTF_CONTENT_TYPE: &str = "application/vnd.ncryptf+json";
8
9pub const NCRYPTF_DRIFT_ALLOWANCE: i32 = 90;
11
12#[derive(Serialize, Deserialize, Clone, Debug)]
16pub struct ExportableEncryptionKeyData {
17 pub public: String,
18 pub signature: String,
19 pub hash_id: String,
20 pub expires_at: i64,
21 pub ephemeral: bool,
22}
23
24impl ExportableEncryptionKeyData {
25 pub fn is_expired(&self) -> bool {
27 return chrono::Utc::now().timestamp() >= self.expires_at;
28 }
29
30 pub fn get_public_key(&self) -> Option<Vec<u8>> {
32 if self.public.is_empty() {
33 return None;
34 }
35
36 return Some(general_purpose::STANDARD.decode(self.public.clone()).unwrap());
37 }
38
39 pub fn get_signature_key(&self) -> Option<Vec<u8>> {
41 if self.public.is_empty() {
42 return None;
43 }
44
45 return Some(general_purpose::STANDARD.decode(self.signature.clone()).unwrap());
46 }
47}
48
49#[derive(Serialize, Deserialize, Debug, Clone)]
51pub struct EncryptionKey {
52 bkp: Keypair,
53 skp: Keypair,
54 ephemeral: bool,
55 pub expires_at: i64,
56 hash_id: String,
57}
58
59impl EncryptionKey {
60 pub fn get_box_kp(&self) -> Keypair {
62 return self.bkp.clone();
63 }
64
65 pub fn get_sign_kp(&self) -> Keypair {
67 return self.skp.clone();
68 }
69
70 pub fn is_ephemeral(&self) -> bool {
72 return self.ephemeral;
73 }
74
75 pub fn get_hash_id(&self) -> String {
77 return self.hash_id.clone();
78 }
79
80 pub fn is_expired(&self) -> bool {
84 if chrono::Utc::now().timestamp() >= self.expires_at {
85 return true;
86 }
87
88 return false;
89 }
90
91 pub fn new(ephemeral: bool) -> Self {
93 let s: String = rand::rng()
94 .sample_iter(&Alphanumeric)
95 .take(24)
96 .map(char::from)
97 .collect();
98
99 let expiration = chrono::Utc::now() + chrono::Duration::hours(1);
101 return Self {
102 bkp: Keypair::new(),
103 skp: Signature::new(),
104 ephemeral: ephemeral,
105 expires_at: expiration.timestamp(),
106 hash_id: s,
107 };
108 }
109}