cryptographic signature, put registration and messages structure
This commit is contained in:
55
client-network/src/cryptographic_signature.rs
Normal file
55
client-network/src/cryptographic_signature.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use p256::ecdsa::{
|
||||
Signature, SigningKey, VerifyingKey,
|
||||
signature::{Signer, Verifier},
|
||||
};
|
||||
use rand_core::OsRng;
|
||||
|
||||
///
|
||||
/// contains the ecdsa private key, the ecdsa public key and the username
|
||||
///
|
||||
pub struct CryptographicSignature {
|
||||
priv_key: SigningKey,
|
||||
pub pub_key: VerifyingKey,
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
impl CryptographicSignature {
|
||||
///
|
||||
/// creates a CryptographicSignature
|
||||
///
|
||||
pub fn new(username: String) -> CryptographicSignature {
|
||||
// generate a private key
|
||||
let priv_key = SigningKey::random(&mut OsRng);
|
||||
// extract the public key from the private key
|
||||
let pub_key = VerifyingKey::from(&priv_key);
|
||||
// return the new struct
|
||||
CryptographicSignature {
|
||||
priv_key: priv_key,
|
||||
pub_key: pub_key,
|
||||
username: username,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// returns a string representing the pub_key as a String
|
||||
///
|
||||
pub fn formatPubKey(crypto_pair: CryptographicSignature) -> String {
|
||||
let encoded_point = crypto_pair.pub_key.to_encoded_point(false);
|
||||
let pubkey_bytes = encoded_point.as_bytes();
|
||||
hex::encode(pubkey_bytes)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||
use super::*;
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user