Add verify() to the Signer trait

And implement it for Blog and User
This commit is contained in:
Trinity Pointard 2018-09-28 23:18:01 +02:00
parent eafe1ed490
commit d610ed1641
3 changed files with 17 additions and 1 deletions

View file

@ -20,6 +20,8 @@ pub trait Signer {
/// Sign some data with the signer keypair /// Sign some data with the signer keypair
fn sign(&self, to_sign: String) -> Vec<u8>; 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 { pub trait Signable {

View file

@ -12,7 +12,7 @@ use openssl::{
hash::MessageDigest, hash::MessageDigest,
pkey::{PKey, Private}, pkey::{PKey, Private},
rsa::Rsa, rsa::Rsa,
sign::Signer sign::{Signer,Verifier}
}; };
use webfinger::*; use webfinger::*;
@ -309,6 +309,13 @@ impl sign::Signer for Blog {
signer.update(to_sign.as_bytes()).unwrap(); signer.update(to_sign.as_bytes()).unwrap();
signer.sign_to_vec().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 { impl NewBlog {

View file

@ -604,6 +604,13 @@ impl Signer for User {
signer.update(to_sign.as_bytes()).unwrap(); signer.update(to_sign.as_bytes()).unwrap();
signer.sign_to_vec().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 { impl NewUser {