Skip to content

Commit b1e7ded

Browse files
authored
Merge branch 'v2.1' into release_2_1.9
2 parents a84bcd5 + 0f117bb commit b1e7ded

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

ext/node/ops/crypto/digest.rs

+20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use digest::DynDigest;
88
use digest::ExtendableOutput;
99
use digest::Update;
1010

11+
mod ring_sha2;
12+
1113
pub struct Hasher {
1214
pub hash: Rc<RefCell<Option<Hash>>>,
1315
}
@@ -200,6 +202,24 @@ impl Hash {
200202
match algorithm_name {
201203
"shake128" => return Ok(Shake128(Default::default(), output_length)),
202204
"shake256" => return Ok(Shake256(Default::default(), output_length)),
205+
"sha256" => {
206+
let digest = ring_sha2::RingSha256::new();
207+
if let Some(length) = output_length {
208+
if length != digest.output_size() {
209+
return Err(HashError::OutputLengthMismatch);
210+
}
211+
}
212+
return Ok(Hash::FixedSize(Box::new(digest)));
213+
}
214+
"sha512" => {
215+
let digest = ring_sha2::RingSha512::new();
216+
if let Some(length) = output_length {
217+
if length != digest.output_size() {
218+
return Err(HashError::OutputLengthMismatch);
219+
}
220+
}
221+
return Ok(Hash::FixedSize(Box::new(digest)));
222+
}
203223
_ => {}
204224
}
205225

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)