Fixed #45, ability to work with old keys and domains.

This commit is contained in:
Revertron
2021-04-15 22:19:45 +02:00
parent 675bb466c1
commit 9d51912733
2 changed files with 20 additions and 5 deletions
+4 -1
View File
@@ -545,7 +545,10 @@ impl Chain {
if let Some(data) = transaction.get_domain_data() {
let b = self.get_block(index - 1).unwrap();
let domain = keystore.decrypt(data.domain.as_slice(), &b.hash.as_slice()[..12]);
let domain = String::from_utf8(domain.to_vec()).unwrap();
let mut domain = String::from_utf8(domain.to_vec()).unwrap();
if domain.is_empty() {
domain = String::from("unknown");
}
trace!("Found my domain {}", domain);
result.insert(domain, (timestamp, data));
}
+16 -4
View File
@@ -2,8 +2,8 @@ use chacha20poly1305::{ChaCha20Poly1305, Key, Nonce};
use chacha20poly1305::aead::{Aead, NewAead};
use std::fmt::{Debug, Formatter};
use std::fmt;
const FAILURE: &str = "encryption failure!";
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
/// A small wrap-up to use Chacha20 encryption for domain names.
#[derive(Clone)]
@@ -20,12 +20,24 @@ impl Chacha {
pub fn encrypt(&self, data: &[u8], nonce: &[u8]) -> Vec<u8> {
let nonce = Nonce::from_slice(nonce);
Vec::from(self.cipher.encrypt(nonce, data.as_ref()).expect(FAILURE))
match self.cipher.encrypt(nonce, data.as_ref()) {
Ok(bytes) => { bytes }
Err(_) => {
warn!("Error encrypting data!");
Vec::new()
}
}
}
pub fn decrypt(&self, data: &[u8], nonce: &[u8]) -> Vec<u8> {
let nonce = Nonce::from_slice(nonce);
Vec::from(self.cipher.decrypt(nonce, data.as_ref()).expect(FAILURE))
match self.cipher.decrypt(nonce, data.as_ref()) {
Ok(bytes) => { bytes }
Err(_) => {
warn!("Error decrypting data!");
Vec::new()
}
}
}
}