Decrypt unknown message
This commit is contained in:
parent
46fded3791
commit
f1c2f0e4ed
@ -398,6 +398,40 @@ pub fn parse_network_msg(raw: String) -> ApiResult<parseNetworkMsgReturn> {
|
|||||||
message: ank_msg.content.to_owned(),
|
message: ank_msg.content.to_owned(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
AnkFlag::Unknown => {
|
||||||
|
let transaction_cache = lock_scanned_transactions()?;
|
||||||
|
// try to decrypt the cipher with all available keys
|
||||||
|
let mut plaintext: String = "".to_owned();
|
||||||
|
for (txid, secret_vec) in transaction_cache.iter() {
|
||||||
|
for (shared_with, ank_secret) in secret_vec.iter() {
|
||||||
|
let shared_secret = ank_secret.to_byte_array();
|
||||||
|
if let Ok(msg_decrypt) = Aes256Decryption::new(
|
||||||
|
Purpose::Arbitrary,
|
||||||
|
ank_msg.content.as_bytes().to_vec(),
|
||||||
|
shared_secret,
|
||||||
|
) {
|
||||||
|
if let Ok(plain) = msg_decrypt.decrypt_with_key() {
|
||||||
|
plaintext = String::from_utf8(plain)?;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if plaintext.is_empty() {
|
||||||
|
// keep the message in cache, just in case
|
||||||
|
// return an error
|
||||||
|
return Err(ApiError {
|
||||||
|
message: "No key found".to_owned(),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// return the plain text
|
||||||
|
return Ok(parseNetworkMsgReturn {
|
||||||
|
topic: AnkFlag::Unknown.as_str().to_owned(),
|
||||||
|
message: plaintext,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -493,13 +527,12 @@ pub fn create_notification_transaction(
|
|||||||
address2secret.push((sp_address.into(), shared_secret));
|
address2secret.push((sp_address.into(), shared_secret));
|
||||||
|
|
||||||
// update our cache
|
// update our cache
|
||||||
lock_scanned_transactions()?
|
lock_scanned_transactions()?.insert(transaction.txid(), address2secret.clone());
|
||||||
.insert(transaction.txid(), address2secret.clone());
|
|
||||||
|
|
||||||
Ok(createNotificationTransactionReturn {
|
Ok(createNotificationTransactionReturn {
|
||||||
txid: transaction.txid().to_string(),
|
txid: transaction.txid().to_string(),
|
||||||
transaction: serialize(&transaction).to_lower_hex_string(),
|
transaction: serialize(&transaction).to_lower_hex_string(),
|
||||||
address2secret: address2secret.into_iter().collect()
|
address2secret: address2secret.into_iter().collect(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -515,7 +548,7 @@ pub struct encryptWithNewKeyResult {
|
|||||||
pub fn encrypt_with_key(plaintext: String, key: String) -> ApiResult<String> {
|
pub fn encrypt_with_key(plaintext: String, key: String) -> ApiResult<String> {
|
||||||
let nonce = Aes256Gcm::generate_nonce(&mut rand::thread_rng());
|
let nonce = Aes256Gcm::generate_nonce(&mut rand::thread_rng());
|
||||||
|
|
||||||
let mut aes_key = [0u8;32];
|
let mut aes_key = [0u8; 32];
|
||||||
aes_key.copy_from_slice(&Vec::from_hex(&key)?);
|
aes_key.copy_from_slice(&Vec::from_hex(&key)?);
|
||||||
|
|
||||||
// encrypt
|
// encrypt
|
||||||
@ -555,30 +588,25 @@ pub fn encrypt_with_new_key(plaintext: String) -> ApiResult<encryptWithNewKeyRes
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn try_decrypt_with_key(
|
pub fn try_decrypt_with_key(cipher: String, key: String) -> ApiResult<String> {
|
||||||
cipher: String,
|
|
||||||
key: String,
|
|
||||||
) -> ApiResult<String> {
|
|
||||||
let key_bin = Vec::from_hex(&key)?;
|
let key_bin = Vec::from_hex(&key)?;
|
||||||
if key_bin.len() != 32 {
|
if key_bin.len() != 32 {
|
||||||
return Err(ApiError { message: "key of invalid lenght".to_owned() });
|
return Err(ApiError {
|
||||||
|
message: "key of invalid lenght".to_owned(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
let mut aes_key = [0u8;32];
|
let mut aes_key = [0u8; 32];
|
||||||
aes_key.copy_from_slice(&Vec::from_hex(&key)?);
|
aes_key.copy_from_slice(&Vec::from_hex(&key)?);
|
||||||
let aes_dec = Aes256Decryption::new(
|
let aes_dec = Aes256Decryption::new(Purpose::Arbitrary, Vec::from_hex(&cipher)?, aes_key)?;
|
||||||
Purpose::Arbitrary,
|
|
||||||
Vec::from_hex(&cipher)?,
|
|
||||||
aes_key
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let plain = String::from_utf8(aes_dec.decrypt_with_key()?)?;
|
let plain = String::from_utf8(aes_dec.decrypt_with_key()?)?;
|
||||||
Ok(plain)
|
Ok(plain)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn create_commitment(payload_to_hash: String) -> String{
|
pub fn create_commitment(payload_to_hash: String) -> String {
|
||||||
let mut engine = sha256::HashEngine::default();
|
let mut engine = sha256::HashEngine::default();
|
||||||
engine.write_all(&payload_to_hash.as_bytes());
|
engine.write_all(&payload_to_hash.as_bytes());
|
||||||
let hash = sha256::Hash::from_engine(engine);
|
let hash = sha256::Hash::from_engine(engine);
|
||||||
String::from_utf8_lossy(hash.to_bytes())
|
hash.to_byte_array().to_lower_hex_string()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user