This commit is contained in:
asonix 2023-08-17 12:03:38 -05:00
parent ff0290a488
commit ec73fe55c9
3 changed files with 119 additions and 0 deletions

View file

@ -15,6 +15,7 @@ default = ["sha-2", "default-spawner"]
middleware = ["dep:reqwest-middleware"] middleware = ["dep:reqwest-middleware"]
default-spawner = ["dep:tokio"] default-spawner = ["dep:tokio"]
digest = ["dep:base64"] digest = ["dep:base64"]
ring = ["digest", "dep:ring"]
sha-2 = ["digest", "dep:sha2"] sha-2 = ["digest", "dep:sha2"]
sha-3 = ["digest", "dep:sha3"] sha-3 = ["digest", "dep:sha3"]
@ -29,6 +30,7 @@ http-signature-normalization = { version = "0.7.0", path = ".." }
httpdate = "1.0.2" httpdate = "1.0.2"
reqwest = { version = "0.11", default-features = false, features = ["json"] } reqwest = { version = "0.11", default-features = false, features = ["json"] }
reqwest-middleware = { version = "0.2.0", optional = true } reqwest-middleware = { version = "0.2.0", optional = true }
ring = { version = "0.16.20", optional = true }
sha2 = { version = "0.10", optional = true } sha2 = { version = "0.10", optional = true }
sha3 = { version = "0.10", optional = true } sha3 = { version = "0.10", optional = true }
thiserror = "1.0" thiserror = "1.0"

View file

@ -2,6 +2,8 @@ use crate::{Config, Sign, SignError, Spawn};
use reqwest::{Body, Request, RequestBuilder}; use reqwest::{Body, Request, RequestBuilder};
use std::fmt::Display; use std::fmt::Display;
#[cfg(feature = "ring")]
pub mod ring;
#[cfg(feature = "sha-2")] #[cfg(feature = "sha-2")]
mod sha2; mod sha2;
#[cfg(feature = "sha-3")] #[cfg(feature = "sha-3")]

115
reqwest/src/digest/ring.rs Normal file
View file

@ -0,0 +1,115 @@
//! Types for creating digests with the `ring` cryptography library
/// A Sha256 digest backed by ring
#[derive(Clone)]
pub struct Sha256 {
ctx: ring::digest::Context,
}
/// A Sha384 digest backed by ring
#[derive(Clone)]
pub struct Sha384 {
ctx: ring::digest::Context,
}
/// A Sha512 digest backed by ring
#[derive(Clone)]
pub struct Sha512 {
ctx: ring::digest::Context,
}
impl Sha256 {
/// Create a new empty digest
pub fn new() -> Self {
Self::default()
}
/// Extract the context
pub fn into_inner(self) -> ring::digest::Context {
self.ctx
}
}
impl Default for Sha256 {
fn default() -> Self {
Sha256 {
ctx: ring::digest::Context::new(&ring::digest::SHA256),
}
}
}
impl Sha384 {
/// Create a new empty digest
pub fn new() -> Self {
Self::default()
}
/// Extract the context
pub fn into_inner(self) -> ring::digest::Context {
self.ctx
}
}
impl Default for Sha384 {
fn default() -> Self {
Sha384 {
ctx: ring::digest::Context::new(&ring::digest::SHA384),
}
}
}
impl Sha512 {
/// Create a new empty digest
pub fn new() -> Self {
Self::default()
}
/// Extract the context
pub fn into_inner(self) -> ring::digest::Context {
self.ctx
}
}
impl Default for Sha512 {
fn default() -> Self {
Sha512 {
ctx: ring::digest::Context::new(&ring::digest::SHA512),
}
}
}
#[cfg(feature = "client")]
mod client {
use super::*;
use crate::digest::DigestCreate;
fn create(mut context: ring::digest::Context, input: &[u8]) -> String {
context.update(input);
let digest = context.finish();
base64::encode(digest.as_ref())
}
impl DigestCreate for Sha256 {
const NAME: &'static str = "SHA-256";
fn compute(&mut self, input: &[u8]) -> String {
create(self.ctx.clone(), input)
}
}
impl DigestCreate for Sha384 {
const NAME: &'static str = "SHA-384";
fn compute(&mut self, input: &[u8]) -> String {
create(self.ctx.clone(), input)
}
}
impl DigestCreate for Sha512 {
const NAME: &'static str = "SHA-512";
fn compute(&mut self, input: &[u8]) -> String {
create(self.ctx.clone(), input)
}
}
}