wip structures and message signature
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
use crate::messages_structure::HandshakeMessage;
|
||||
use p256::ecdsa::{
|
||||
Signature, SigningKey, VerifyingKey,
|
||||
signature::{Signer, Verifier},
|
||||
};
|
||||
use rand_core::OsRng;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
pub enum MathError {
|
||||
DivisionByZero,
|
||||
NonPositiveLogarithm,
|
||||
NegativeSquareRoot,
|
||||
}
|
||||
|
||||
///
|
||||
/// contains the ecdsa private key, the ecdsa public key and the username
|
||||
@@ -40,16 +48,52 @@ pub fn formatPubKey(crypto_pair: CryptographicSignature) -> String {
|
||||
hex::encode(pubkey_bytes)
|
||||
}
|
||||
|
||||
pub fn sign_message(crypto_pair: CryptographicSignature, message: [u8; 1024]) -> [u8; 1024] {
|
||||
let digest = Sha256::digest(&message[0..992]);
|
||||
let str = hex::encode(digest);
|
||||
let signature = crypto_pair.priv_key.sign_prehash_recoverable(str.as_bytes());
|
||||
let mut signed_message = [0;1024];
|
||||
signed_message[..992].copy_from_slice(&message[..992]);
|
||||
match signature {
|
||||
Ok(signature) => {
|
||||
println!("Signature: {:?}", signature);
|
||||
let r = signature.0.r();
|
||||
|
||||
let r_bytes = r.to_bytes(); // Returns a GenericArray/bytes object
|
||||
|
||||
signed_message[992..].copy_from_slice(&r_bytes[..32]);
|
||||
signed_message
|
||||
},
|
||||
Err(e) => {
|
||||
panic!("error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
/*#[test]
|
||||
fn creating_cryptographic_signature() {
|
||||
let username = String::from("quoicoubeh");
|
||||
let crypto_pair = CryptographicSignature::new(username);
|
||||
let formatted_pubkey =formatPubKey(crypto_pair);
|
||||
println!("pubkey : {}",formatted_pubkey);
|
||||
}*/
|
||||
|
||||
#[test]
|
||||
fn signing_message() {
|
||||
let username = String::from("quoicoubeh");
|
||||
let crypto_pair = CryptographicSignature::new(username);
|
||||
let username_b = String::from("quoicoubeh");
|
||||
let handshake = HandshakeMessage::hello(0, 12, username_b);
|
||||
let ser = handshake.serialize();
|
||||
let signed_message = sign_message(crypto_pair, ser);
|
||||
println!("unsigned_message: {:?}", ser);
|
||||
println!("signed_message: {:?}", signed_message);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user