Add verify() to the Signer trait
And implement it for Blog and User
This commit is contained in:
parent
eafe1ed490
commit
d610ed1641
|
@ -20,6 +20,8 @@ pub trait Signer {
|
|||
|
||||
/// Sign some data with the signer keypair
|
||||
fn sign(&self, to_sign: String) -> Vec<u8>;
|
||||
/// Verify if the signature is valid
|
||||
fn verify(&self, data: String, signature: Vec<u8>) -> bool;
|
||||
}
|
||||
|
||||
pub trait Signable {
|
||||
|
|
|
@ -12,7 +12,7 @@ use openssl::{
|
|||
hash::MessageDigest,
|
||||
pkey::{PKey, Private},
|
||||
rsa::Rsa,
|
||||
sign::Signer
|
||||
sign::{Signer,Verifier}
|
||||
};
|
||||
use webfinger::*;
|
||||
|
||||
|
@ -309,6 +309,13 @@ impl sign::Signer for Blog {
|
|||
signer.update(to_sign.as_bytes()).unwrap();
|
||||
signer.sign_to_vec().unwrap()
|
||||
}
|
||||
|
||||
fn verify(&self, data: String, signature: Vec<u8>) -> bool {
|
||||
let key = PKey::from_rsa(Rsa::public_key_from_pem(self.public_key.as_ref()).unwrap()).unwrap();
|
||||
let mut verifier = Verifier::new(MessageDigest::sha256(), &key).unwrap();
|
||||
verifier.update(data.as_bytes()).unwrap();
|
||||
verifier.verify(&signature).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl NewBlog {
|
||||
|
|
|
@ -604,6 +604,13 @@ impl Signer for User {
|
|||
signer.update(to_sign.as_bytes()).unwrap();
|
||||
signer.sign_to_vec().unwrap()
|
||||
}
|
||||
|
||||
fn verify(&self, data: String, signature: Vec<u8>) -> bool {
|
||||
let key = PKey::from_rsa(Rsa::public_key_from_pem(self.public_key.as_ref()).unwrap()).unwrap();
|
||||
let mut verifier = sign::Verifier::new(MessageDigest::sha256(), &key).unwrap();
|
||||
verifier.update(data.as_bytes()).unwrap();
|
||||
verifier.verify(&signature).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl NewUser {
|
||||
|
|
Loading…
Reference in New Issue