IronForce is a decentralized network, Degeon is a messenger built on it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.9 KiB

3 years ago
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
/// Public key of a node
3 years ago
#[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!()
3 years ago
}
/// Encrypt some data for a user with this public key
pub fn encrypt_data(&self, _data: &[u8]) -> Vec<u8> {
todo!()
3 years ago
}
}
/// Key pair (public and secret) for a node, should be stored locally
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Keys {}
3 years ago
impl Keys {
/// Generate new random key
pub fn generate() -> Keys {
todo!()
3 years ago
}
}
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!()
3 years ago
}
/// Get public key
3 years ago
pub fn get_public(&self) -> PublicKey {
todo!()
3 years ago
}
}
#[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)));
}