Encoding: Refactor export

This commit is contained in:
K Shiva Kiran 2024-04-09 22:18:20 +05:30
parent e69cd8c2a7
commit c08ad88303
2 changed files with 11 additions and 15 deletions

View File

@ -11,8 +11,7 @@ fn main() {
let key2 = fb.add(msg2.as_bytes()); let key2 = fb.add(msg2.as_bytes());
// Export as base64 // Export as base64
let cipher = fb.export_cipher(); let (cipher, keybase) = fb.export(); // Careful with the order
let keybase = fb.export_keybase();
let key1_exp = key1.export(); let key1_exp = key1.export();
// Or as raw bytes // Or as raw bytes
let key2_exp = key2.to_bytes(); let key2_exp = key2.to_bytes();

View File

@ -7,24 +7,21 @@ where
Self: FBObjTrait<T>, Self: FBObjTrait<T>,
T: ArrayEncoding + Bounded, T: ArrayEncoding + Bounded,
{ {
fn to_bytes_cipher(&self) -> Vec<u8> { fn to_bytes(&self) -> (Vec<u8>, Vec<u8>) {
self.cipher().iter() let c = self.cipher().iter()
.flat_map(|bigint| bigint.to_le_byte_array()) .flat_map(|bigint| bigint.to_le_byte_array())
.collect() .collect();
} let r = self.keybase().iter()
fn to_bytes_keybase(&self) -> Vec<u8> {
self.keybase().iter()
.flat_map(|bigint| bigint.to_le_byte_array()) .flat_map(|bigint| bigint.to_le_byte_array())
.collect() .collect();
(c, r)
} }
fn export_cipher(&self) -> String { fn export(&self) -> (String, String) {
BASE64_STANDARD.encode(self.to_bytes_cipher()) let (c, r) = self.to_bytes();
}
fn export_keybase(&self) -> String { (BASE64_STANDARD.encode(c), BASE64_STANDARD.encode(r))
BASE64_STANDARD.encode(self.to_bytes_keybase())
} }
fn from_bytes(cipher: &[u8], keybase: &[u8]) -> Result<FBObj<T>, FBError> { fn from_bytes(cipher: &[u8], keybase: &[u8]) -> Result<FBObj<T>, FBError> {