56 lines
1.5 KiB
Rust
56 lines
1.5 KiB
Rust
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);
|
|
}
|
|
}
|