Added second keypair for encryption of domain names.

Changed keys file format to include second pair of keys, it will be in TOML now.
Made many adjustments to block and transaction structures.
Changed block serialization to binary format for hashing/mining.
Removed old build dependencies.
This commit is contained in:
Revertron
2021-05-04 16:47:03 +02:00
parent 52695e0988
commit bc6d2fbae3
19 changed files with 349 additions and 249 deletions
+88
View File
@@ -0,0 +1,88 @@
use ecies_ed25519::{SecretKey, PublicKey, Error, encrypt, decrypt};
use rand_old::{CryptoRng, RngCore};
use std::fmt::{Debug, Formatter};
use crate::{to_hex, from_hex};
use std::fmt;
pub struct CryptoBox {
pub(crate) secret: SecretKey,
pub(crate) public: PublicKey,
}
impl CryptoBox {
pub fn new(seed: &[u8]) -> Self {
let secret = SecretKey::from_bytes(seed).expect("Unable to parse secret key");
let public = PublicKey::from_secret(&secret);
Self { secret, public }
}
pub fn generate<R>(csprng: &mut R) -> Self where R: CryptoRng + RngCore {
let (secret, public) = ecies_ed25519::generate_keypair(csprng);
Self { secret, public }
}
pub fn from_strings(secret: &str, public: &str) -> Self {
let secret = SecretKey::from_bytes(&from_hex(secret).unwrap()).unwrap();
let public = PublicKey::from_bytes(&from_hex(public).unwrap()).unwrap();
Self { secret, public }
}
pub fn hide(&self, msg: &[u8]) -> Result<Vec<u8>, Error> {
let mut random = rand_old::thread_rng();
encrypt(&self.public, msg, &mut random)
}
pub fn reveal(&self, msg: &[u8]) -> Result<Vec<u8>, Error> {
decrypt(&self.secret, msg)
}
pub fn encrypt(public: &[u8], message: &[u8]) -> Result<Vec<u8>, Error> {
let public = PublicKey::from_bytes(public).unwrap();
let mut random = rand_old::thread_rng();
encrypt(&public, message, &mut random)
}
pub fn decrypt(secret: &[u8], message: &[u8]) -> Result<Vec<u8>, Error> {
let secret = SecretKey::from_bytes(secret).unwrap();
decrypt(&secret, &message)
}
}
impl Debug for CryptoBox {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("CryptoBox")
.field("public", &to_hex(&self.public.to_bytes()))
.finish()
}
}
impl Clone for CryptoBox {
fn clone(&self) -> Self {
let secret = SecretKey::from_bytes(&self.secret.as_bytes()[..]).expect("Unable clone secret key");
let public = PublicKey::from_secret(&secret);
Self { secret, public }
}
}
#[cfg(test)]
mod tests {
use rand::RngCore;
use crate::{to_hex, from_hex};
use ed25519_dalek::Keypair;
use crate::crypto::CryptoBox;
use ecies_ed25519::{encrypt, decrypt, SecretKey, PublicKey};
const TEXT: &str = "Some very secret message";
#[test]
pub fn hide_reveal() {
let mut rng = rand::thread_rng();
let mut buf = [0u8; 32];
rng.fill_bytes(&mut buf);
let coder = CryptoBox::new(&buf);
let encrypted = coder.hide(TEXT.as_bytes()).unwrap();
let decrypted = coder.reveal(&encrypted.as_slice()).unwrap();
assert_eq!(TEXT, &String::from_utf8(decrypted).unwrap());
}
}