From 491c5fe9bd5e4538fdd95f4e31b9d742fea0e25b Mon Sep 17 00:00:00 2001 From: ennucore Date: Sat, 28 Mar 2020 21:40:06 +0300 Subject: [PATCH] Tests for crypto --- src/crypto.rs | 16 +++++++++++----- src/lib.rs | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index 9527e39..70f87f1 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -15,6 +15,16 @@ pub struct PublicKey { pub key: [u8; 32] } +impl PublicKey { + pub fn verify_sign(&self, raw: &Vec, sign: &Vec) -> bool { + PK::from_bytes(&self.key).unwrap() + .verify( + raw, + &Signature::from_bytes(sign.clone().into_boxed_slice().as_ref()).unwrap(), + ).is_ok() + } +} + pub struct Keys { pub pair: Keypair, @@ -31,11 +41,7 @@ pub fn verify_sign(msg: &crate::message::Message) -> bool { match &msg.clone().sign { Sign::NoSign => true, Sign::Signed(key, sign) => { - PK::from_bytes(&key.key).unwrap() - .verify( - msg.get_hash().as_ref(), - &Signature::from_bytes(sign.clone().into_boxed_slice().as_ref()).unwrap(), - ).is_ok() + key.verify_sign(&msg.get_hash(), sign) } } } diff --git a/src/lib.rs b/src/lib.rs index a3b5fa6..bea47d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,30 @@ mod tests { } } + mod crypto { + use crate::crypto::{PublicKey, Keys, verify_sign}; + + #[test] + fn creation() { + let key_pair = Keys::gen(); + } + + #[test] + fn signing() { + let key_pair = Keys::gen(); + key_pair.sign(&[1u8; 129].to_vec()); + } + + #[test] + fn verification() { + let key_pair = Keys::gen(); + let sign = key_pair.sign(&[1u8; 129].to_vec()); + assert!(key_pair.get_public().verify_sign(&[1u8; 129].to_vec(), &sign)); + assert!(!key_pair.get_public().verify_sign(&[0u8; 129].to_vec(), &sign)); + assert!(!key_pair.get_public().verify_sign(&[1u8; 129].to_vec(), &[0u8; 64].to_vec())); + } + } + mod message { use crate::message::{MsgType, Message}; use crate::crypto::Keys;