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 ed25519_dalek::{Keypair, PublicKey as PK}; |
||||
use rand::rngs::OsRng; |
||||
use serde::{Deserialize, Serialize}; |
||||
|
||||
/// Public key of a node
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
||||
pub struct PublicKey { |
||||
pub key: [u8; 32], |
||||
} |
||||
|
||||
impl PublicKey { |
||||
pub fn verify_sign(&self, raw: &Vec<u8>, sign: &Vec<u8>) -> bool { |
||||
PK::from_bytes(&self.key) |
||||
.unwrap() |
||||
.verify( |
||||
raw, |
||||
&Signature::from_bytes(sign.clone().into_boxed_slice().as_ref()).unwrap(), |
||||
) |
||||
.is_ok() |
||||
/// Check if the sign is valid for gived data and key
|
||||
pub fn verify_sign(&self, _data: &[u8], _sign: &[u8]) -> bool { |
||||
todo!() |
||||
} |
||||
} |
||||
|
||||
pub struct Keys { |
||||
pub pair: Keypair, |
||||
} |
||||
|
||||
impl Keys { |
||||
pub fn gen() -> Keys { |
||||
Keys { |
||||
pair: Keypair::generate(&mut OsRng {}), |
||||
} |
||||
/// Encrypt some data for a user with this public key
|
||||
pub fn encrypt_data(&self, _data: &[u8]) -> Vec<u8> { |
||||
todo!() |
||||
} |
||||
} |
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
||||
pub enum Sign { |
||||
NoSign, |
||||
Signed(PublicKey /* source */, Vec<u8> /* sign */), |
||||
} |
||||
/// Key pair (public and secret) for a node, should be stored locally
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)] |
||||
pub struct Keys {} |
||||
|
||||
pub fn verify_sign(data: &Vec<u8>, sign: &Sign) -> bool { |
||||
match sign { |
||||
Sign::NoSign => true, |
||||
Sign::Signed(key, sign) => key.verify_sign(data, sign), |
||||
impl Keys { |
||||
/// Generate new random key
|
||||
pub fn generate() -> Keys { |
||||
todo!() |
||||
} |
||||
} |
||||
|
||||
impl Keys { |
||||
pub fn sign(&self, content: &Vec<u8>) -> Vec<u8> { |
||||
self.pair.sign(content.as_ref()).to_bytes().to_vec() |
||||
/// 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 { |
||||
PublicKey { |
||||
key: *self.clone().pair.public.as_bytes(), |
||||
} |
||||
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))); |
||||
} |
||||
|
Loading…
Reference in new issue