|
|
|
use alloc::vec::Vec;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
/// Public key of a node
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
|
|
|
pub struct PublicKey {
|
|
|
|
pub key: [u8; 32],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PublicKey {
|
|
|
|
/// Check if the sign is valid for gived data and key
|
|
|
|
pub fn verify_sign(&self, _data: &[u8], _sign: &[u8]) -> bool {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encrypt some data for a user with this public key
|
|
|
|
pub fn encrypt_data(&self, _data: &[u8]) -> Vec<u8> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Key pair (public and secret) for a node, should be stored locally
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub struct Keys {}
|
|
|
|
|
|
|
|
impl Keys {
|
|
|
|
/// Generate new random key
|
|
|
|
pub fn generate() -> Keys {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Keys {
|
|
|
|
/// Sign content using these keys
|
|
|
|
pub fn sign(&self, _content: &[u8]) -> Vec<u8> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decrypt data
|
|
|
|
pub fn decrypt_data(&self, _data_encrypted: &[u8]) -> Option<Vec<u8>> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get public key
|
|
|
|
pub fn get_public(&self) -> PublicKey {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_encrypt() {
|
|
|
|
let data = vec![0, 5, 8, 135, 67];
|
|
|
|
let keys = Keys::generate();
|
|
|
|
assert_eq!(
|
|
|
|
keys.decrypt_data(keys.get_public().encrypt_data(data)),
|
|
|
|
Some(data)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_encrypt() {
|
|
|
|
let data = vec![0, 5, 8, 135, 67];
|
|
|
|
let keys_1 = Keys::generate();
|
|
|
|
let keys_2 = Keys::generate();
|
|
|
|
assert!(keys_2.decrypt_data(keys_1.get_public().encrypt_data(data)) != Some(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signing() {
|
|
|
|
let data = vec![0, 5, 8, 135, 67];
|
|
|
|
let keys = Keys::generate();
|
|
|
|
assert!(keys.get_public().verify_sign(data, keys.sign(data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_signing() {
|
|
|
|
let data = vec![0, 5, 8, 135, 67];
|
|
|
|
let keys_1 = Keys::generate();
|
|
|
|
let keys_2 = Keys::generate();
|
|
|
|
assert!(keys_2.get_public().verify_sign(data, keys_1.sign(data)));
|
|
|
|
}
|