// SPDX-License-Identifier: GPL-3.0-or-later use crate::FBError; use base64::prelude::{BASE64_STANDARD, Engine}; use bincode::{Options, DefaultOptions}; /// A key object that is specific to a message. pub struct FBKey { pub(crate) indices: Vec>, } impl FBKey { /// Returns the byte representation of the key. pub fn to_bytes(&self) -> Vec { let binc = DefaultOptions::new(); binc.serialize(&self.indices) .expect("Should be fine") } /// Returns the base64 encoded representation of the key. pub fn export(&self) -> String { BASE64_STANDARD.encode(&self.to_bytes()) } /// Constructs the key from the provided bytes. /// # Errors /// [DecodeError](FBError::DecodeError) pub fn from_bytes(fbkey: &[u8]) -> Result { let binc = DefaultOptions::new(); let indices: Vec<_> = binc.deserialize(&fbkey) .map_err(|_| FBError::DecodeError)?; if indices.len() < 2 { return Err(FBError::DecodeError); } Ok (FBKey {indices}) } /// Constructs the key from the provided base64 encoded form. /// # Errors /// [DecodeError](FBError::DecodeError) pub fn import(key_str: &str) -> Result { let indice_bytes = BASE64_STANDARD.decode(key_str) .map_err(|_| FBError::DecodeError)?; Self::from_bytes(&indice_bytes) } }