mirror of
https://git.asonix.dog/asonix/http-signature-normalization.git
synced 2024-06-11 17:59:33 +00:00
116 lines
2.3 KiB
Rust
116 lines
2.3 KiB
Rust
|
//! 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)
|
||
|
}
|
||
|
}
|
||
|
}
|