Lev
3 years ago
2 changed files with 59 additions and 39 deletions
@ -1,63 +1,81 @@ |
|||||||
extern crate rand; |
|
||||||
|
|
||||||
use crate::ed25519_dalek::ed25519::signature::Signature; |
|
||||||
use crate::ed25519_dalek::Signer; |
|
||||||
use crate::ed25519_dalek::Verifier; |
|
||||||
use alloc::vec::Vec; |
use alloc::vec::Vec; |
||||||
use ed25519_dalek::{Keypair, PublicKey as PK}; |
|
||||||
use rand::rngs::OsRng; |
|
||||||
use serde::{Deserialize, Serialize}; |
use serde::{Deserialize, Serialize}; |
||||||
|
|
||||||
|
/// Public key of a node
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
||||||
pub struct PublicKey { |
pub struct PublicKey { |
||||||
pub key: [u8; 32], |
pub key: [u8; 32], |
||||||
} |
} |
||||||
|
|
||||||
impl PublicKey { |
impl PublicKey { |
||||||
pub fn verify_sign(&self, raw: &Vec<u8>, sign: &Vec<u8>) -> bool { |
/// Check if the sign is valid for gived data and key
|
||||||
PK::from_bytes(&self.key) |
pub fn verify_sign(&self, _data: &[u8], _sign: &[u8]) -> bool { |
||||||
.unwrap() |
todo!() |
||||||
.verify( |
|
||||||
raw, |
|
||||||
&Signature::from_bytes(sign.clone().into_boxed_slice().as_ref()).unwrap(), |
|
||||||
) |
|
||||||
.is_ok() |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
pub struct Keys { |
/// Encrypt some data for a user with this public key
|
||||||
pub pair: Keypair, |
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 { |
impl Keys { |
||||||
pub fn gen() -> Keys { |
/// Generate new random key
|
||||||
Keys { |
pub fn generate() -> Keys { |
||||||
pair: Keypair::generate(&mut OsRng {}), |
todo!() |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
|
impl Keys { |
||||||
|
/// Sign content using these keys
|
||||||
|
pub fn sign(&self, _content: &[u8]) -> Vec<u8> { |
||||||
|
todo!() |
||||||
} |
} |
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
/// Decrypt data
|
||||||
pub enum Sign { |
pub fn decrypt_data(&self, _data_encrypted: &[u8]) -> Option<Vec<u8>> { |
||||||
NoSign, |
todo!() |
||||||
Signed(PublicKey /* source */, Vec<u8> /* sign */), |
|
||||||
} |
} |
||||||
|
|
||||||
pub fn verify_sign(data: &Vec<u8>, sign: &Sign) -> bool { |
/// Get public key
|
||||||
match sign { |
pub fn get_public(&self) -> PublicKey { |
||||||
Sign::NoSign => true, |
todo!() |
||||||
Sign::Signed(key, sign) => key.verify_sign(data, sign), |
|
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
impl Keys { |
#[test] |
||||||
pub fn sign(&self, content: &Vec<u8>) -> Vec<u8> { |
fn test_encrypt() { |
||||||
self.pair.sign(content.as_ref()).to_bytes().to_vec() |
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) |
||||||
|
); |
||||||
} |
} |
||||||
|
|
||||||
pub fn get_public(&self) -> PublicKey { |
#[test] |
||||||
PublicKey { |
fn test_invalid_encrypt() { |
||||||
key: *self.clone().pair.public.as_bytes(), |
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))); |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue