Skip to content

Commit

Permalink
changed from message pack encoding to base64 encoding
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Den Hartog <kdenhar@gmail.com>
  • Loading branch information
kdenhartog committed Jan 11, 2019
1 parent cc5c4fd commit 9f4707b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
12 changes: 0 additions & 12 deletions libindy/src/domain/crypto/combo_box.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
extern crate rmp_serde;

#[derive(Serialize, Deserialize, Debug)]
pub struct ComboBox {
pub msg: String,
pub sender: String,
pub nonce: String
}

impl ComboBox {
pub fn to_msg_pack(&self) -> Result<Vec<u8>, rmp_serde::encode::Error> {
rmp_serde::encode::to_vec_named(self)
}

pub fn from_msg_pack(bytes: &[u8]) -> Result<ComboBox, rmp_serde::decode::Error> {
rmp_serde::decode::from_slice(bytes)
}
}
35 changes: 29 additions & 6 deletions libindy/src/services/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,42 @@ impl CryptoService {
};

//TODO check about removing msg_pack dependency
let msg_pack = combo_box.to_msg_pack()
.map_err(|e| CommonError::InvalidState(format!("Can't serialize ComboBox: {:?}", e)))?;

let res = self.crypto_box_seal(&their_vk, &msg_pack)?;
let combo_box_str = serde_json::to_string(&combo_box)
.map_err(|err|
CryptoError::CommonError(
CommonError::InvalidStructure(
format!("Failed to encode combo_box {}", err)
)
)
)?;
let combo_box_encoded = base64::encode(combo_box_str.as_bytes());
let res = self.crypto_box_seal(&their_vk, combo_box_encoded.as_bytes())?;

Ok(res)
}

pub fn authenticated_decrypt(&self, my_key: &Key, doc: &[u8]) -> Result<(String, Vec<u8>), CryptoError> {
let decrypted_msg = self.crypto_box_seal_open(&my_key, &doc)?;

let parsed_msg = ComboBox::from_msg_pack(decrypted_msg.as_slice())
.map_err(|err| CommonError::InvalidStructure(format!("Can't deserialize ComboBox: {:?}", err)))?;
let combo_box_encoded_str = String::from_utf8(decrypted_msg)
.map_err(|err|
CryptoError::CommonError(
CommonError::InvalidStructure(
format!("failed to decode combo_box to utf-8 {}", err)
)
)
)?;
let combo_box_decoded = base64::decode(&combo_box_encoded_str)
.map_err(|err| CryptoError::CommonError(err))?;

let parsed_msg : ComboBox = serde_json::from_slice(combo_box_decoded.as_slice())
.map_err(|err|
CryptoError::CommonError(
CommonError::InvalidStructure(
format!("Failed to serialize combo_box {}", err)
)
)
)?;

let doc: Vec<u8> = base64::decode(&parsed_msg.msg)
.map_err(|err| CommonError::InvalidStructure(format!("Can't decode internal msg filed from base64 {}", err)))?;
Expand Down

0 comments on commit 9f4707b

Please sign in to comment.