2021-09-23 22:05:11 +00:00
|
|
|
use crate::digest::DigestName;
|
2021-12-13 18:35:10 +00:00
|
|
|
use sha2::{Sha224, Sha256, Sha384, Sha512};
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestName for Sha224 {
|
|
|
|
const NAME: &'static str = "SHA-244";
|
2020-03-17 19:27:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestName for Sha256 {
|
|
|
|
const NAME: &'static str = "SHA-256";
|
2020-03-17 19:27:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestName for Sha384 {
|
|
|
|
const NAME: &'static str = "SHA-384";
|
|
|
|
}
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestName for Sha512 {
|
|
|
|
const NAME: &'static str = "SHA-512";
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
#[cfg(feature = "client")]
|
|
|
|
mod client {
|
|
|
|
use super::*;
|
|
|
|
use crate::digest::DigestCreate;
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2021-12-13 18:35:10 +00:00
|
|
|
fn create<D: sha2::Digest + sha2::digest::FixedOutputReset>(
|
|
|
|
digest: &mut D,
|
|
|
|
input: &[u8],
|
|
|
|
) -> String {
|
|
|
|
sha2::Digest::update(digest, input);
|
2021-09-23 22:05:11 +00:00
|
|
|
base64::encode(&digest.finalize_reset())
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestCreate for Sha224 {
|
|
|
|
fn compute(&mut self, input: &[u8]) -> String {
|
|
|
|
create(self, input)
|
|
|
|
}
|
2020-07-01 22:27:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestCreate for Sha256 {
|
|
|
|
fn compute(&mut self, input: &[u8]) -> String {
|
|
|
|
create(self, input)
|
|
|
|
}
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestCreate for Sha384 {
|
|
|
|
fn compute(&mut self, input: &[u8]) -> String {
|
|
|
|
create(self, input)
|
|
|
|
}
|
|
|
|
}
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestCreate for Sha512 {
|
|
|
|
fn compute(&mut self, input: &[u8]) -> String {
|
|
|
|
create(self, input)
|
|
|
|
}
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
#[cfg(feature = "server")]
|
|
|
|
mod server {
|
|
|
|
use super::*;
|
|
|
|
use crate::digest::{DigestPart, DigestVerify};
|
|
|
|
use tracing::{debug, warn};
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2021-12-13 18:35:10 +00:00
|
|
|
fn verify<D: sha2::Digest + sha2::digest::FixedOutputReset>(
|
|
|
|
digest: &mut D,
|
|
|
|
name: &str,
|
|
|
|
parts: &[DigestPart],
|
|
|
|
) -> bool {
|
2021-09-23 22:05:11 +00:00
|
|
|
if let Some(part) = parts
|
|
|
|
.iter()
|
|
|
|
.find(|p| p.algorithm.to_lowercase() == name.to_lowercase())
|
|
|
|
{
|
|
|
|
debug!("Verifying digest type, {}", name);
|
|
|
|
let encoded = base64::encode(&digest.finalize_reset());
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
return part.digest == encoded;
|
|
|
|
}
|
|
|
|
warn!("No matching digest algorithm found for {}", name);
|
|
|
|
warn!(
|
|
|
|
"Provided: [{}]",
|
|
|
|
parts.iter().fold(String::new(), |mut acc, item| {
|
|
|
|
if acc.is_empty() {
|
|
|
|
} else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(&item.algorithm);
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
);
|
2020-07-01 22:27:34 +00:00
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
false
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestVerify for Sha224 {
|
|
|
|
fn update(&mut self, part: &[u8]) {
|
|
|
|
sha2::Digest::update(self, part);
|
|
|
|
}
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
|
|
|
verify(self, Self::NAME, parts)
|
|
|
|
}
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestVerify for Sha256 {
|
|
|
|
fn update(&mut self, part: &[u8]) {
|
|
|
|
sha2::Digest::update(self, part);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
|
|
|
verify(self, Self::NAME, parts)
|
|
|
|
}
|
2020-07-01 22:27:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestVerify for Sha384 {
|
|
|
|
fn update(&mut self, part: &[u8]) {
|
|
|
|
sha2::Digest::update(self, part);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
|
|
|
verify(self, Self::NAME, parts)
|
|
|
|
}
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
impl DigestVerify for Sha512 {
|
|
|
|
fn update(&mut self, part: &[u8]) {
|
|
|
|
sha2::Digest::update(self, part);
|
|
|
|
}
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2021-09-23 22:05:11 +00:00
|
|
|
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
|
|
|
verify(self, Self::NAME, parts)
|
|
|
|
}
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
}
|