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
11pub(crate) struct Sha256(Context);
12
13impl Sha256 {
14    pub(crate) fn new() -> Self {
15        Self(Context::new(&SHA256))
16    }
17
18    pub(crate) fn update(&mut self, chunk: &[u8]) {
19        self.0.update(chunk);
20    }
21
22    pub(crate) fn finish(self) -> [u8; 32] {
23        let digest = self.0.finish();
24        digest.as_ref().try_into().expect("sha256 hash is 32 bytes")
25    }
26}