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(), |
/// Encrypt some data for a user with this public key
|
||||||
) |
pub fn encrypt_data(&self, _data: &[u8]) -> Vec<u8> { |
||||||
.is_ok() |
todo!() |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
pub struct Keys { |
/// Key pair (public and secret) for a node, should be stored locally
|
||||||
pub pair: Keypair, |
#[derive(Debug, Serialize, Deserialize, Clone)] |
||||||
|
pub struct Keys {} |
||||||
|
|
||||||
|
impl Keys { |
||||||
|
/// Generate new random key
|
||||||
|
pub fn generate() -> Keys { |
||||||
|
todo!() |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
impl Keys { |
impl Keys { |
||||||
pub fn gen() -> Keys { |
/// Sign content using these keys
|
||||||
Keys { |
pub fn sign(&self, _content: &[u8]) -> Vec<u8> { |
||||||
pair: Keypair::generate(&mut OsRng {}), |
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!() |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
#[test] |
||||||
pub enum Sign { |
fn test_encrypt() { |
||||||
NoSign, |
let data = vec![0, 5, 8, 135, 67]; |
||||||
Signed(PublicKey /* source */, Vec<u8> /* sign */), |
let keys = Keys::generate(); |
||||||
|
assert_eq!( |
||||||
|
keys.decrypt_data(keys.get_public().encrypt_data(data)), |
||||||
|
Some(data) |
||||||
|
); |
||||||
} |
} |
||||||
|
|
||||||
pub fn verify_sign(data: &Vec<u8>, sign: &Sign) -> bool { |
#[test] |
||||||
match sign { |
fn test_invalid_encrypt() { |
||||||
Sign::NoSign => true, |
let data = vec![0, 5, 8, 135, 67]; |
||||||
Sign::Signed(key, sign) => key.verify_sign(data, sign), |
let keys_1 = Keys::generate(); |
||||||
} |
let keys_2 = Keys::generate(); |
||||||
|
assert!(keys_2.decrypt_data(keys_1.get_public().encrypt_data(data)) != Some(data)); |
||||||
} |
} |
||||||
|
|
||||||
impl Keys { |
#[test] |
||||||
pub fn sign(&self, content: &Vec<u8>) -> Vec<u8> { |
fn test_signing() { |
||||||
self.pair.sign(content.as_ref()).to_bytes().to_vec() |
let data = vec![0, 5, 8, 135, 67]; |
||||||
} |
let keys = Keys::generate(); |
||||||
|
assert!(keys.get_public().verify_sign(data, keys.sign(data))); |
||||||
|
} |
||||||
|
|
||||||
pub fn get_public(&self) -> PublicKey { |
#[test] |
||||||
PublicKey { |
fn test_invalid_signing() { |
||||||
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.get_public().verify_sign(data, keys_1.sign(data))); |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue