Skip to main content

async_nats/
crypto.rs

1#[cfg(feature = "aws-lc-rs")]
2use aws_lc_rs as crypto_backend;
3#[cfg(not(feature = "aws-lc-rs"))]
4use ring as crypto_backend;
5
6#[cfg(not(any(feature = "aws-lc-rs", feature = "ring")))]
7compile_error!("Please enable the `aws-lc-rs` or `ring` feature");
8
9use crypto_backend::digest::{Context, SHA256};
10
11#[allow(dead_code)]
12pub(crate) struct Sha256(Context);
13
14#[allow(dead_code)]
15impl Sha256 {
16    pub(crate) fn new() -> Self {
17        Self(Context::new(&SHA256))
18    }
19
20    pub(crate) fn update(&mut self, chunk: &[u8]) {
21        self.0.update(chunk);
22    }
23
24    pub(crate) fn finish(self) -> [u8; 32] {
25        let digest = self.0.finish();
26        digest.as_ref().try_into().expect("sha256 hash is 32 bytes")
27    }
28}