Browse Source

Tests for crypto

master
ennucore 5 years ago
parent
commit
491c5fe9bd
  1. 16
      src/crypto.rs
  2. 24
      src/lib.rs

16
src/crypto.rs

@ -15,6 +15,16 @@ 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()
}
}
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)
}
}
}

24
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;

Loading…
Cancel
Save