Added functionality to import/export in raw bytes

This commit is contained in:
K Shiva Kiran 2024-03-28 20:04:19 +05:30
parent 6286051e12
commit e69cd8c2a7
3 changed files with 55 additions and 38 deletions

View File

@ -10,16 +10,18 @@ fn main() {
let msg2 = "This is another message";
let key2 = fb.add(msg2.as_bytes());
// Export
// Export as base64
let cipher = fb.export_cipher();
let keybase = fb.export_keybase();
let keybase = fb.export_keybase();
let key1_exp = key1.export();
let key2_exp = key2.export();
// Or as raw bytes
let key2_exp = key2.to_bytes();
// Import
// Import from base64
let fb_new = FB128::import(&cipher, &keybase).unwrap();
let key1_imp = FBKey::import(&key1_exp).unwrap();
let key2_imp = FBKey::import(&key2_exp).unwrap();
// Or as raw bytes
let key2_imp = FBKey::from_bytes(&key2_exp).unwrap();
// Decryption
let decr1 = fb_new.decrypt(&key1_imp).unwrap();
@ -30,7 +32,7 @@ fn main() {
CipherText: \n{cipher}\n
KeyBase: \n{keybase}\n
Key 1: {key1_exp}
Key 2: {key2_exp}
Key 2: {key2_exp:?}
");
assert_eq!(msg1.as_bytes(), decr1);

View File

@ -4,39 +4,38 @@ use base64::{prelude::BASE64_STANDARD, Engine};
pub trait Encode<T>
where
Self: FBObjTrait<T>,
T: ArrayEncoding + Bounded,
Self: FBObjTrait<T>,
T: ArrayEncoding + Bounded,
{
fn export_cipher(&self) -> String {
let c_bytes: Vec<u8> = self.cipher().iter()
fn to_bytes_cipher(&self) -> Vec<u8> {
self.cipher().iter()
.flat_map(|bigint| bigint.to_le_byte_array())
.collect();
BASE64_STANDARD.encode(c_bytes)
.collect()
}
fn export_keybase(&self) -> String {
let r_bytes: Vec<u8> = self.keybase().iter()
fn to_bytes_keybase(&self) -> Vec<u8> {
self.keybase().iter()
.flat_map(|bigint| bigint.to_le_byte_array())
.collect();
.collect()
}
BASE64_STANDARD.encode(r_bytes)
}
fn export_cipher(&self) -> String {
BASE64_STANDARD.encode(self.to_bytes_cipher())
}
fn import(cipher: &str, keybase: &str) -> Result<FBObj<T>, FBError> {
let c_bytes = BASE64_STANDARD.decode(cipher)
.map_err(|_| FBError::DecodeError)?;
let r_bytes = BASE64_STANDARD.decode(keybase)
.map_err(|_| FBError::DecodeError)?;
fn export_keybase(&self) -> String {
BASE64_STANDARD.encode(self.to_bytes_keybase())
}
let chunk_to_uint = |chunk| {
T::from_le_byte_array(
GenericArray::clone_from_slice(chunk)
)};
let c: Vec<T> = c_bytes.chunks_exact(T::BYTES)
fn from_bytes(cipher: &[u8], keybase: &[u8]) -> Result<FBObj<T>, FBError> {
let chunk_to_uint = |chunk| {
T::from_le_byte_array(
GenericArray::clone_from_slice(chunk)
)};
let c: Vec<T> = cipher.chunks_exact(T::BYTES)
.map(chunk_to_uint)
.collect();
let r: Vec<T> = r_bytes.chunks_exact(T::BYTES)
let r: Vec<T> = keybase.chunks_exact(T::BYTES)
.map(chunk_to_uint)
.collect();
if r.len() > c.len() || r.len() < 2 {
@ -45,4 +44,13 @@ where
Ok(FBObj {c, r})
}
fn import(cipher: &str, keybase: &str) -> Result<FBObj<T>, FBError> {
let c_bytes = BASE64_STANDARD.decode(cipher)
.map_err(|_| FBError::DecodeError)?;
let r_bytes = BASE64_STANDARD.decode(keybase)
.map_err(|_| FBError::DecodeError)?;
Self::from_bytes(&c_bytes, &r_bytes)
}
}

View File

@ -8,19 +8,19 @@ pub struct FBKey {
impl FBKey {
pub fn export(&self) -> String {
pub fn to_bytes(&self) -> Vec<u8> {
let binc = DefaultOptions::new();
let indice_bytes = binc.serialize(&self.indices)
.unwrap();
BASE64_STANDARD.encode(&indice_bytes)
binc.serialize(&self.indices)
.expect("Should be fine")
}
pub fn import(key_str: &str) -> Result<FBKey, FBError> {
pub fn export(&self) -> String {
BASE64_STANDARD.encode(&self.to_bytes())
}
pub fn from_bytes(fbkey: &[u8]) -> Result<FBKey, FBError> {
let binc = DefaultOptions::new();
let indice_bytes = BASE64_STANDARD.decode(key_str)
.map_err(|_| FBError::DecodeError)?;
let indices: Vec<_> = binc.deserialize(&indice_bytes)
let indices: Vec<_> = binc.deserialize(&fbkey)
.map_err(|_| FBError::DecodeError)?;
if indices.len() < 2 {
return Err(FBError::DecodeError);
@ -28,4 +28,11 @@ impl FBKey {
Ok (FBKey {indices})
}
pub fn import(key_str: &str) -> Result<FBKey, FBError> {
let indice_bytes = BASE64_STANDARD.decode(key_str)
.map_err(|_| FBError::DecodeError)?;
Self::from_bytes(&indice_bytes)
}
}