From c08ad883035ea615015b99744f86d317b482956c Mon Sep 17 00:00:00 2001 From: K Shiva Kiran Date: Tue, 9 Apr 2024 22:18:20 +0530 Subject: [PATCH] Encoding: Refactor export --- examples/export.rs | 3 +-- src/encoding.rs | 23 ++++++++++------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/examples/export.rs b/examples/export.rs index 035ecb0..6bb3d46 100644 --- a/examples/export.rs +++ b/examples/export.rs @@ -11,8 +11,7 @@ fn main() { let key2 = fb.add(msg2.as_bytes()); // Export as base64 - let cipher = fb.export_cipher(); - let keybase = fb.export_keybase(); + let (cipher, keybase) = fb.export(); // Careful with the order let key1_exp = key1.export(); // Or as raw bytes let key2_exp = key2.to_bytes(); diff --git a/src/encoding.rs b/src/encoding.rs index 0ba7a1c..3ffe5ec 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -7,24 +7,21 @@ where Self: FBObjTrait, T: ArrayEncoding + Bounded, { - fn to_bytes_cipher(&self) -> Vec { - self.cipher().iter() + fn to_bytes(&self) -> (Vec, Vec) { + let c = self.cipher().iter() .flat_map(|bigint| bigint.to_le_byte_array()) - .collect() - } - - fn to_bytes_keybase(&self) -> Vec { - self.keybase().iter() + .collect(); + let r = self.keybase().iter() .flat_map(|bigint| bigint.to_le_byte_array()) - .collect() + .collect(); + + (c, r) } - fn export_cipher(&self) -> String { - BASE64_STANDARD.encode(self.to_bytes_cipher()) - } + fn export(&self) -> (String, String) { + let (c, r) = self.to_bytes(); - fn export_keybase(&self) -> String { - BASE64_STANDARD.encode(self.to_bytes_keybase()) + (BASE64_STANDARD.encode(c), BASE64_STANDARD.encode(r)) } fn from_bytes(cipher: &[u8], keybase: &[u8]) -> Result, FBError> {