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

201 lines
4.7 KiB
Rust
Raw Normal View History

2019-09-13 01:12:35 +00:00
use sha3::{
Keccak224, Keccak256, Keccak256Full, Keccak384, Keccak512, Sha3_224, Sha3_256, Sha3_384,
Sha3_512,
2019-09-13 01:12:35 +00:00
};
2021-09-18 00:34:16 +00:00
use tracing::{debug, warn};
2019-09-13 01:12:35 +00:00
use super::{DigestCreate, DigestPart, DigestVerify};
2020-09-30 03:09:35 +00:00
fn create(digest: &mut impl sha3::Digest, input: &[u8]) -> String {
2020-06-10 22:15:19 +00:00
digest.update(input);
base64::encode(&digest.finalize_reset())
}
2020-09-30 03:09:35 +00:00
fn verify(digest: &mut impl sha3::Digest, name: &str, parts: &[DigestPart]) -> bool {
if let Some(part) = parts
.iter()
.find(|p| p.algorithm.to_lowercase() == name.to_lowercase())
{
2020-03-20 01:08:33 +00:00
debug!("Verifying digest type, {}", name);
2020-06-10 22:15:19 +00:00
let encoded = base64::encode(&digest.finalize_reset());
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 {
2020-04-26 01:41:21 +00:00
acc.push_str(", ");
}
2020-04-26 01:41:21 +00:00
acc.push_str(&item.algorithm);
acc
})
);
false
}
2019-09-13 01:12:35 +00:00
impl DigestCreate for Sha3_224 {
const NAME: &'static str = "SHA3-224";
2019-09-13 01:12:35 +00:00
fn compute(&mut self, input: &[u8]) -> String {
create(self, input)
2019-09-13 01:12:35 +00:00
}
}
impl DigestVerify for Sha3_224 {
fn update(&mut self, part: &[u8]) {
sha3::Digest::update(self, part);
}
fn verify(&mut self, parts: &[DigestPart]) -> bool {
verify(self, <Self as DigestCreate>::NAME, parts)
2019-09-13 01:12:35 +00:00
}
}
impl DigestCreate for Sha3_256 {
const NAME: &'static str = "SHA3-256";
2019-09-13 01:12:35 +00:00
fn compute(&mut self, input: &[u8]) -> String {
create(self, input)
2019-09-13 01:12:35 +00:00
}
}
impl DigestVerify for Sha3_256 {
fn update(&mut self, part: &[u8]) {
sha3::Digest::update(self, part);
}
fn verify(&mut self, parts: &[DigestPart]) -> bool {
verify(self, <Self as DigestCreate>::NAME, parts)
2019-09-13 01:12:35 +00:00
}
}
impl DigestCreate for Sha3_384 {
const NAME: &'static str = "SHA3-384";
2019-09-13 01:12:35 +00:00
fn compute(&mut self, input: &[u8]) -> String {
create(self, input)
2019-09-13 01:12:35 +00:00
}
}
impl DigestVerify for Sha3_384 {
fn update(&mut self, part: &[u8]) {
sha3::Digest::update(self, part);
}
fn verify(&mut self, parts: &[DigestPart]) -> bool {
verify(self, <Self as DigestCreate>::NAME, parts)
2019-09-13 01:12:35 +00:00
}
}
impl DigestCreate for Sha3_512 {
const NAME: &'static str = "SHA3-512";
2019-09-13 01:12:35 +00:00
fn compute(&mut self, input: &[u8]) -> String {
create(self, input)
2019-09-13 01:12:35 +00:00
}
}
impl DigestVerify for Sha3_512 {
fn update(&mut self, part: &[u8]) {
sha3::Digest::update(self, part);
}
fn verify(&mut self, parts: &[DigestPart]) -> bool {
verify(self, <Self as DigestCreate>::NAME, parts)
2019-09-13 01:12:35 +00:00
}
}
impl DigestCreate for Keccak224 {
const NAME: &'static str = "keccak-224";
fn compute(&mut self, input: &[u8]) -> String {
create(self, input)
2019-09-13 01:12:35 +00:00
}
}
impl DigestVerify for Keccak224 {
fn update(&mut self, part: &[u8]) {
sha3::Digest::update(self, part);
}
fn verify(&mut self, parts: &[DigestPart]) -> bool {
verify(self, <Self as DigestCreate>::NAME, parts)
2019-09-13 01:12:35 +00:00
}
}
impl DigestCreate for Keccak256 {
const NAME: &'static str = "keccak-256";
fn compute(&mut self, input: &[u8]) -> String {
create(self, input)
2019-09-13 01:12:35 +00:00
}
}
impl DigestVerify for Keccak256 {
fn update(&mut self, part: &[u8]) {
sha3::Digest::update(self, part);
}
fn verify(&mut self, parts: &[DigestPart]) -> bool {
verify(self, <Self as DigestCreate>::NAME, parts)
2019-09-13 01:12:35 +00:00
}
}
impl DigestCreate for Keccak256Full {
const NAME: &'static str = "keccak-256-full";
fn compute(&mut self, input: &[u8]) -> String {
create(self, input)
2019-09-13 01:12:35 +00:00
}
}
impl DigestVerify for Keccak256Full {
fn update(&mut self, part: &[u8]) {
sha3::Digest::update(self, part);
}
fn verify(&mut self, parts: &[DigestPart]) -> bool {
verify(self, <Self as DigestCreate>::NAME, parts)
2019-09-13 01:12:35 +00:00
}
}
impl DigestCreate for Keccak384 {
const NAME: &'static str = "keccak-384";
fn compute(&mut self, input: &[u8]) -> String {
create(self, input)
2019-09-13 01:12:35 +00:00
}
}
impl DigestVerify for Keccak384 {
fn update(&mut self, part: &[u8]) {
sha3::Digest::update(self, part);
}
fn verify(&mut self, parts: &[DigestPart]) -> bool {
verify(self, <Self as DigestCreate>::NAME, parts)
2019-09-13 01:12:35 +00:00
}
}
impl DigestCreate for Keccak512 {
const NAME: &'static str = "keccak-512";
fn compute(&mut self, input: &[u8]) -> String {
create(self, input)
2019-09-13 01:12:35 +00:00
}
}
impl DigestVerify for Keccak512 {
fn update(&mut self, part: &[u8]) {
sha3::Digest::update(self, part);
}
fn verify(&mut self, parts: &[DigestPart]) -> bool {
verify(self, <Self as DigestCreate>::NAME, parts)
2019-09-13 01:12:35 +00:00
}
}