http-signature-normalization/http-signature-normalization-actix/src/digest/sha2.rs

154 lines
3.8 KiB
Rust
Raw Normal View History

2019-09-13 01:12:35 +00:00
use sha2::{Digest as _, Sha224, Sha256, Sha384, Sha512, Sha512Trunc224, Sha512Trunc256};
use super::{DigestCreate, DigestPart, DigestVerify};
impl DigestCreate for Sha224 {
const NAME: &'static str = "sha-224";
fn compute(&mut self, input: &[u8]) -> String {
self.input(input);
base64::encode(&self.result_reset())
}
}
impl DigestVerify for Sha224 {
fn verify(&mut self, parts: &[DigestPart], bytes: &[u8]) -> bool {
if let Some(part) = parts
.iter()
.find(|p| p.algorithm == <Self as DigestCreate>::NAME)
{
self.input(bytes);
let digest = base64::encode(&self.result_reset());
return part.digest == digest;
}
false
}
}
impl DigestCreate for Sha256 {
const NAME: &'static str = "sha-256";
fn compute(&mut self, input: &[u8]) -> String {
self.input(input);
base64::encode(&self.result_reset())
}
}
impl DigestVerify for Sha256 {
fn verify(&mut self, parts: &[DigestPart], bytes: &[u8]) -> bool {
if let Some(part) = parts
.iter()
.find(|p| p.algorithm == <Self as DigestCreate>::NAME)
{
self.input(bytes);
let digest = base64::encode(&self.result_reset());
return part.digest == digest;
}
false
}
}
impl DigestCreate for Sha384 {
const NAME: &'static str = "sha-384";
fn compute(&mut self, input: &[u8]) -> String {
self.input(input);
base64::encode(&self.result_reset())
}
}
impl DigestVerify for Sha384 {
fn verify(&mut self, parts: &[DigestPart], bytes: &[u8]) -> bool {
if let Some(part) = parts
.iter()
.find(|p| p.algorithm == <Self as DigestCreate>::NAME)
{
self.input(bytes);
let digest = base64::encode(&self.result_reset());
return part.digest == digest;
}
false
}
}
impl DigestCreate for Sha512 {
const NAME: &'static str = "sha-512";
fn compute(&mut self, input: &[u8]) -> String {
self.input(input);
base64::encode(&self.result_reset())
}
}
impl DigestVerify for Sha512 {
fn verify(&mut self, parts: &[DigestPart], bytes: &[u8]) -> bool {
if let Some(part) = parts
.iter()
.find(|p| p.algorithm == <Self as DigestCreate>::NAME)
{
self.input(bytes);
let digest = base64::encode(&self.result_reset());
return part.digest == digest;
}
false
}
}
impl DigestCreate for Sha512Trunc224 {
const NAME: &'static str = "sha-512-224";
fn compute(&mut self, input: &[u8]) -> String {
self.input(input);
base64::encode(&self.result_reset())
}
}
impl DigestVerify for Sha512Trunc224 {
fn verify(&mut self, parts: &[DigestPart], bytes: &[u8]) -> bool {
if let Some(part) = parts
.iter()
.find(|p| p.algorithm == <Self as DigestCreate>::NAME)
{
self.input(bytes);
let digest = base64::encode(&self.result_reset());
return part.digest == digest;
}
false
}
}
impl DigestCreate for Sha512Trunc256 {
const NAME: &'static str = "sha-512-256";
fn compute(&mut self, input: &[u8]) -> String {
self.input(input);
base64::encode(&self.result_reset())
}
}
impl DigestVerify for Sha512Trunc256 {
fn verify(&mut self, parts: &[DigestPart], bytes: &[u8]) -> bool {
if let Some(part) = parts
.iter()
.find(|p| p.algorithm == <Self as DigestCreate>::NAME)
{
self.input(bytes);
let digest = base64::encode(&self.result_reset());
return part.digest == digest;
}
false
}
}