Use ring for reqwest example

This commit is contained in:
asonix 2023-08-17 12:34:51 -05:00
parent ec73fe55c9
commit c1d36000dd
3 changed files with 29 additions and 34 deletions

View file

@ -21,7 +21,7 @@ sha-3 = ["digest", "dep:sha3"]
[[example]] [[example]]
name = "client" name = "client"
required-features = ["default-spawner", "sha-2"] required-features = ["default-spawner", "ring"]
[dependencies] [dependencies]
async-trait = "0.1.71" async-trait = "0.1.71"

View file

@ -1,9 +1,8 @@
use http_signature_normalization_reqwest::prelude::*; use http_signature_normalization_reqwest::{digest::ring::Sha256, prelude::*};
use reqwest::{ use reqwest::{
header::{ACCEPT, USER_AGENT}, header::{ACCEPT, USER_AGENT},
Client, Client,
}; };
use sha2::{Digest, Sha256};
async fn request(config: Config) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { async fn request(config: Config) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let digest = Sha256::new(); let digest = Sha256::new();

View file

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