Browse Source

Signatures and working

master
ennucore 5 years ago
parent
commit
01839b8937
  1. 19
      src/crypto.rs
  2. 12
      src/lib.rs
  3. 31
      src/message.rs

19
src/crypto.rs

@ -7,9 +7,10 @@ use sha2::Sha512;
use serde::{Serialize, Deserialize};
use rand::rngs::OsRng;
use self::ed25519_dalek::Digest;
use alloc::vec::Vec;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct PublicKey {
pub key: [u8; 32]
}
@ -28,17 +29,21 @@ impl KeyPack {
}
impl PublicKey {
pub fn verify_sign(self, msg: crate::message::Message, sign: Signature) -> bool {
PK::from_bytes(&self.key).unwrap().verify(msg.get_hash().as_ref(), &sign).is_ok()
pub fn verify_sign(&self, msg: &crate::message::Message) -> bool {
PK::from_bytes(&self.key).unwrap()
.verify(
msg.get_hash().as_ref(),
&Signature::from_bytes(&msg.clone().sign.into_boxed_slice()).unwrap()
).is_ok()
}
}
impl KeyPack {
pub fn sign(self, msg: u64) -> u64 {
0
pub fn sign(&self, msg: &crate::message::Message) -> Vec<u8> {
self.pair.sign(msg.hash.as_ref()).to_bytes().to_vec()
}
pub fn get_public(self) -> PublicKey {
PublicKey { key: *self.pair.public.as_bytes() }
pub fn get_public(&self) -> PublicKey {
PublicKey { key: *self.clone().pair.public.as_bytes() }
}
}

12
src/lib.rs

@ -26,17 +26,25 @@ mod tests {
fn serialization() {
let key_pack = KeyPack::gen();
let msg = Message::new(
MsgType::MultiCast, Vec::<u8>::new(), key_pack
MsgType::MultiCast, Vec::<u8>::new(), &key_pack
);
let serialized = &msg.ser();
let msg2 = Message::deserialize(serialized.to_vec());
assert_eq!(msg2.msg_type, MsgType::MultiCast);
}
#[test]
fn valid_message_creation() {
let msg = Message::new(
MsgType::MultiCast, Vec::<u8>::new(), &KeyPack::gen()
);
assert!(msg.verify());
}
#[test]
fn test_hash() {
let msg = Message::new(
MsgType::MultiCast, Vec::<u8>::new(), crate::crypto::KeyPack::gen()
MsgType::MultiCast, Vec::<u8>::new(), &crate::crypto::KeyPack::gen()
);
msg.get_hash();
}

31
src/message.rs

@ -3,36 +3,49 @@ use sha2::Digest;
use serde::{Serialize, Deserialize};
use alloc::vec::Vec;
use pinecone::{from_bytes, to_vec};
use serde::private::de::size_hint::from_bounds;
use ed25519_dalek::Signature;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub enum MsgType {
MultiCast,
ToTarget(PublicKey),
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Message {
pub msg_type: MsgType,
pub body: Vec<u8>,
pub source: PublicKey,
hash: u64,
sign: u64,
pub hash: Vec<u8>,
pub sign: Vec<u8>,
}
impl Message {
pub fn get_hash(self) -> Vec<u8> {
pub fn get_hash(&self) -> Vec<u8> {
let mut hasher = sha2::Sha256::new();
hasher.input(self.ser());
let msg2 = Message {
msg_type: self.msg_type.clone(),
body: self.body.clone(),
source: self.source.clone(),
hash: Vec::<u8>::new(),
sign: Vec::<u8>::new()
};
hasher.input(msg2.ser());
hasher.result().to_vec()
}
pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: KeyPack) -> Message {
let msg = Message { msg_type, body, source: key_pack.get_public(), hash: 0, sign: 0 };
pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: &KeyPack) -> Message {
let mut msg = Message { msg_type, body, source: key_pack.get_public(), hash: Vec::<u8>::new(), sign: Vec::<u8>::new() };
msg.hash = msg.get_hash();
msg.sign = key_pack.sign(&msg);
msg
}
pub fn verify(self) -> bool {
self.hash == self.get_hash() && self.source.verify_sign(&self)
}
pub fn ser(self) -> Vec<u8> {
to_vec(&self).expect("Serialization failed")
}

Loading…
Cancel
Save