dji_log_parser/keychain/
mod.rs

1use base64::engine::general_purpose::STANDARD as Base64Standard;
2use base64::Engine as _;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5#[cfg(target_arch = "wasm32")]
6use tsify_next::Tsify;
7
8mod api;
9mod feature_point;
10
11pub use api::*;
12pub use feature_point::FeaturePoint;
13
14#[derive(Debug, Serialize, Clone)]
15#[serde(rename_all = "camelCase")]
16#[cfg_attr(target_arch = "wasm32", derive(Tsify))]
17pub struct EncodedKeychainFeaturePoint {
18    pub feature_point: FeaturePoint,
19    pub aes_ciphertext: String,
20}
21
22#[derive(Debug, Serialize, Deserialize, Clone)]
23#[serde(rename_all = "camelCase")]
24#[cfg_attr(target_arch = "wasm32", derive(Tsify))]
25pub struct KeychainFeaturePoint {
26    pub feature_point: FeaturePoint,
27    pub aes_key: String,
28    pub aes_iv: String,
29}
30
31/// `Keychain` serves as more convenient access to decrypt `Record` instances.
32/// It associates each `FeaturePoint` with its corresponding AES initialization vector (IV)
33/// and encryption key. In this hashmap, each `FeaturePoint` is linked to a tuple containing
34/// the AES IV and key as array of bytes.
35pub(crate) struct Keychain(HashMap<FeaturePoint, (Vec<u8>, Vec<u8>)>);
36
37impl Keychain {
38    pub fn empty() -> Self {
39        Keychain(HashMap::new())
40    }
41
42    pub fn from_feature_points(keychain_entries: &Vec<KeychainFeaturePoint>) -> Self {
43        Keychain(
44            keychain_entries
45                .into_iter()
46                .map(|entry| {
47                    (
48                        entry.feature_point,
49                        (
50                            Base64Standard.decode(&entry.aes_iv).unwrap_or_default(),
51                            Base64Standard.decode(&entry.aes_key).unwrap_or_default(),
52                        ),
53                    )
54                })
55                .collect(),
56        )
57    }
58
59    pub fn get(&self, key: &FeaturePoint) -> Option<&(Vec<u8>, Vec<u8>)> {
60        self.0.get(key)
61    }
62
63    pub fn insert(
64        &mut self,
65        key: FeaturePoint,
66        value: (Vec<u8>, Vec<u8>),
67    ) -> Option<(Vec<u8>, Vec<u8>)> {
68        self.0.insert(key, value)
69    }
70}