Digest: add verify methods

This commit is contained in:
Aode (lion) 2021-11-19 17:54:54 -06:00
parent fba736e7f7
commit 36255bfbb4
3 changed files with 22 additions and 0 deletions

View file

@ -3,6 +3,8 @@ pub trait Digest {
fn build() -> Self;
fn digest(self, input: &[u8]) -> String;
fn update(&mut self, input: &[u8]);
fn verify(self, encoded: &str) -> bool;
}
pub trait DigestFactory {

View file

@ -51,6 +51,16 @@ impl<'a> apub_core::digest::Digest for OpenSslDigest {
openssl::base64::encode_block(&bytes)
}
fn update(&mut self, input: &[u8]) {
self.digest.update(input);
}
fn verify(self, encoded: &str) -> bool {
let bytes = self.digest.finish();
openssl::base64::encode_block(&bytes) == encoded
}
}
impl apub_core::signature::Sign for OpenSslSigner {

View file

@ -60,6 +60,16 @@ impl apub_core::digest::Digest for Sha256Digest {
base64::encode(&bytes)
}
fn update(&mut self, input: &[u8]) {
self.digest.update(input);
}
fn verify(self, encoded: &str) -> bool {
let bytes = self.digest.finalize();
base64::encode(&bytes) == encoded
}
}
impl apub_core::signature::Sign for RsaSigner {