encoding: Renamed export methods
This commit is contained in:
parent
70214e1b6f
commit
b75355eed3
|
@ -11,14 +11,14 @@ fn main() {
|
||||||
let key2 = fb.add(msg2.as_bytes());
|
let key2 = fb.add(msg2.as_bytes());
|
||||||
|
|
||||||
// Export as base64
|
// Export as base64
|
||||||
let (cipher, keybase) = fb.export(); // Careful with the order
|
let (cipher, keybase) = fb.as_base64(); // Careful with the order
|
||||||
let key1_exp = key1.export();
|
let key1_exp = key1.as_base64();
|
||||||
// Or as raw bytes
|
// Or as raw bytes
|
||||||
let key2_exp = key2.to_bytes();
|
let key2_exp = key2.as_bytes();
|
||||||
|
|
||||||
// Import from base64
|
// Import from base64
|
||||||
let fb_new = FB128::import(&cipher, &keybase).unwrap();
|
let fb_new = FB128::from_base64(&cipher, &keybase).unwrap();
|
||||||
let key1_imp = FBKey::import(&key1_exp).unwrap();
|
let key1_imp = FBKey::from_base64(&key1_exp).unwrap();
|
||||||
// Or as raw bytes
|
// Or as raw bytes
|
||||||
let key2_imp = FBKey::from_bytes(&key2_exp).unwrap();
|
let key2_imp = FBKey::from_bytes(&key2_exp).unwrap();
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ where
|
||||||
T: ArrayEncoding + Bounded,
|
T: ArrayEncoding + Bounded,
|
||||||
{
|
{
|
||||||
/// Returns the byte representation of the ciphertext and keybase.
|
/// Returns the byte representation of the ciphertext and keybase.
|
||||||
fn to_bytes(&self) -> (Vec<u8>, Vec<u8>) {
|
fn as_bytes(&self) -> (Vec<u8>, Vec<u8>) {
|
||||||
let c = self.cipher().read().unwrap()
|
let c = self.cipher().read().unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|bigint| bigint.to_le_byte_array())
|
.flat_map(|bigint| bigint.to_le_byte_array())
|
||||||
|
@ -23,8 +23,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the base64 encoded representation of the ciphertext and keybase.
|
/// Returns the base64 encoded representation of the ciphertext and keybase.
|
||||||
fn export(&self) -> (String, String) {
|
fn as_base64(&self) -> (String, String) {
|
||||||
let (c, r) = self.to_bytes();
|
let (c, r) = self.as_bytes();
|
||||||
|
|
||||||
(BASE64_STANDARD.encode(c), BASE64_STANDARD.encode(r))
|
(BASE64_STANDARD.encode(c), BASE64_STANDARD.encode(r))
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ where
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// - [DecodeError](FBError::DecodeError)
|
/// - [DecodeError](FBError::DecodeError)
|
||||||
/// - [InvalidParams](FBError::InvalidParams) - Are the parameters in the wrong order?
|
/// - [InvalidParams](FBError::InvalidParams) - Are the parameters in the wrong order?
|
||||||
fn import(cipher: &str, keybase: &str) -> Result<FBObj<T>, FBError> {
|
fn from_base64(cipher: &str, keybase: &str) -> Result<FBObj<T>, FBError> {
|
||||||
let c_bytes = BASE64_STANDARD.decode(cipher)
|
let c_bytes = BASE64_STANDARD.decode(cipher)
|
||||||
.map_err(|_| FBError::DecodeError)?;
|
.map_err(|_| FBError::DecodeError)?;
|
||||||
let r_bytes = BASE64_STANDARD.decode(keybase)
|
let r_bytes = BASE64_STANDARD.decode(keybase)
|
||||||
|
|
|
@ -11,15 +11,15 @@ pub struct FBKey {
|
||||||
impl FBKey {
|
impl FBKey {
|
||||||
|
|
||||||
/// Returns the byte representation of the key.
|
/// Returns the byte representation of the key.
|
||||||
pub fn to_bytes(&self) -> Vec<u8> {
|
pub fn as_bytes(&self) -> Vec<u8> {
|
||||||
let binc = DefaultOptions::new();
|
let binc = DefaultOptions::new();
|
||||||
binc.serialize(&self.indices)
|
binc.serialize(&self.indices)
|
||||||
.expect("Should be fine")
|
.expect("Should be fine")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the base64 encoded representation of the key.
|
/// Returns the base64 encoded representation of the key.
|
||||||
pub fn export(&self) -> String {
|
pub fn as_base64(&self) -> String {
|
||||||
BASE64_STANDARD.encode(&self.to_bytes())
|
BASE64_STANDARD.encode(&self.as_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Constructs the key from the provided bytes.
|
/// Constructs the key from the provided bytes.
|
||||||
|
@ -39,7 +39,7 @@ impl FBKey {
|
||||||
/// Constructs the key from the provided base64 encoded form.
|
/// Constructs the key from the provided base64 encoded form.
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// [DecodeError](FBError::DecodeError)
|
/// [DecodeError](FBError::DecodeError)
|
||||||
pub fn import(key_str: &str) -> Result<FBKey, FBError> {
|
pub fn from_base64(key_str: &str) -> Result<FBKey, FBError> {
|
||||||
let indice_bytes = BASE64_STANDARD.decode(key_str)
|
let indice_bytes = BASE64_STANDARD.decode(key_str)
|
||||||
.map_err(|_| FBError::DecodeError)?;
|
.map_err(|_| FBError::DecodeError)?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue