|
| 1 | +// Copyright 2018-2025 the Deno authors. MIT license. |
| 2 | + |
| 3 | +use std::marker::PhantomData; |
| 4 | + |
| 5 | +use digest::generic_array::ArrayLength; |
| 6 | + |
| 7 | +pub trait RingDigestAlgo { |
| 8 | + fn algorithm() -> &'static ring::digest::Algorithm; |
| 9 | + type OutputSize: ArrayLength<u8> + 'static; |
| 10 | +} |
| 11 | + |
| 12 | +pub struct RingDigest<Algo: RingDigestAlgo> { |
| 13 | + context: ring::digest::Context, |
| 14 | + _phantom: PhantomData<Algo>, |
| 15 | +} |
| 16 | + |
| 17 | +impl<Algo: RingDigestAlgo> Clone for RingDigest<Algo> { |
| 18 | + fn clone(&self) -> Self { |
| 19 | + Self { |
| 20 | + context: self.context.clone(), |
| 21 | + _phantom: self._phantom, |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl<Algo: RingDigestAlgo> digest::HashMarker for RingDigest<Algo> {} |
| 27 | +impl<Algo: RingDigestAlgo> Default for RingDigest<Algo> { |
| 28 | + fn default() -> Self { |
| 29 | + Self { |
| 30 | + context: ring::digest::Context::new(Algo::algorithm()), |
| 31 | + _phantom: PhantomData, |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +impl<Algo: RingDigestAlgo> digest::Reset for RingDigest<Algo> { |
| 36 | + fn reset(&mut self) { |
| 37 | + self.context = ring::digest::Context::new(Algo::algorithm()) |
| 38 | + } |
| 39 | +} |
| 40 | +impl<Algo: RingDigestAlgo> digest::Update for RingDigest<Algo> { |
| 41 | + fn update(&mut self, data: &[u8]) { |
| 42 | + self.context.update(data); |
| 43 | + } |
| 44 | +} |
| 45 | +impl<Algo: RingDigestAlgo> digest::OutputSizeUser for RingDigest<Algo> { |
| 46 | + type OutputSize = Algo::OutputSize; |
| 47 | +} |
| 48 | +impl<Algo: RingDigestAlgo> digest::FixedOutput for RingDigest<Algo> { |
| 49 | + fn finalize_into(self, out: &mut digest::Output<Self>) { |
| 50 | + let result = self.context.finish(); |
| 51 | + out.copy_from_slice(result.as_ref()); |
| 52 | + } |
| 53 | +} |
| 54 | +impl<Algo: RingDigestAlgo> digest::FixedOutputReset for RingDigest<Algo> { |
| 55 | + fn finalize_into_reset(&mut self, out: &mut digest::Output<Self>) { |
| 56 | + let context = std::mem::replace( |
| 57 | + &mut self.context, |
| 58 | + ring::digest::Context::new(Algo::algorithm()), |
| 59 | + ); |
| 60 | + out.copy_from_slice(context.finish().as_ref()); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +pub struct RingSha256Algo; |
| 65 | +impl RingDigestAlgo for RingSha256Algo { |
| 66 | + fn algorithm() -> &'static ring::digest::Algorithm { |
| 67 | + &ring::digest::SHA256 |
| 68 | + } |
| 69 | + |
| 70 | + type OutputSize = digest::typenum::U32; |
| 71 | +} |
| 72 | +pub struct RingSha512Algo; |
| 73 | +impl RingDigestAlgo for RingSha512Algo { |
| 74 | + fn algorithm() -> &'static ring::digest::Algorithm { |
| 75 | + &ring::digest::SHA512 |
| 76 | + } |
| 77 | + |
| 78 | + type OutputSize = digest::typenum::U64; |
| 79 | +} |
| 80 | + |
| 81 | +pub type RingSha256 = RingDigest<RingSha256Algo>; |
| 82 | +pub type RingSha512 = RingDigest<RingSha512Algo>; |
0 commit comments