| 212 | |
| 213 | #[tracing::instrument(level = "debug", skip(self, key))] |
| 214 | pub fn get_secure<T>(&self, key: String) -> Result<T> |
| 215 | where |
| 216 | T: DeserializeOwned, |
| 217 | { |
| 218 | #[cfg(not(any(target_os = "android", target_os = "ios")))] |
| 219 | { |
| 220 | let data: String = self.load_selective(key.clone())?; |
| 221 | let mut split = data.split(':'); |
| 222 | let nonce = split.next().unwrap(); |
| 223 | let nonce = GenericArray::clone_from_slice(&hex::decode(nonce).unwrap()[0..12]); |
| 224 | let ciphertext = hex::decode(split.next().unwrap()).unwrap(); |
| 225 | |
| 226 | let secret = self.secret.lock().unwrap(); |
| 227 | let cipher = ChaCha20Poly1305::new(&secret); |
| 228 | let plaintext = String::from_utf8( |
| 229 | cipher |
| 230 | .decrypt(&nonce, ciphertext.as_slice()) |
| 231 | .map_err(|e| MoosyncError::String(e.to_string()))?, |
| 232 | )?; |
| 233 | |
| 234 | Ok(serde_json::from_str(&plaintext)?) |
| 235 | } |
| 236 | |
| 237 | #[cfg(any(target_os = "android", target_os = "ios"))] |
| 238 | { |
| 239 | self.load_selective(key.clone()) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | #[tracing::instrument(level = "debug", skip(self, key, value))] |
| 244 | pub fn set_secure<T>(&self, key: String, value: Option<T>) -> Result<()> |