veilid_core/crypto/crypto_system/
blake3digest512.rs1use curve25519_dalek::digest::generic_array::{typenum::U64, GenericArray};
2use curve25519_dalek::digest::{
3 Digest, FixedOutput, FixedOutputReset, Output, OutputSizeUser, Reset, Update,
4};
5
6pub struct Blake3Digest512 {
7 dig: blake3::Hasher,
8}
9
10impl OutputSizeUser for Blake3Digest512 {
11 type OutputSize = U64;
12}
13
14impl Update for Blake3Digest512 {
15 fn update(&mut self, data: &[u8]) {
16 self.dig.update(data);
17 }
18}
19
20impl FixedOutput for Blake3Digest512 {
21 fn finalize_into(self, out: &mut Output<Self>) {
22 let mut b = [0u8; 64];
23 self.dig.finalize_xof().fill(&mut b);
24 out[0..64].copy_from_slice(&b[0..64]);
25 }
26}
27
28impl Reset for Blake3Digest512 {
29 fn reset(&mut self) {
30 self.dig.reset();
31 }
32}
33
34impl FixedOutputReset for Blake3Digest512 {
35 fn finalize_into_reset(&mut self, out: &mut Output<Self>) {
36 let mut b = [0u8; 64];
37 self.dig.finalize_xof().fill(&mut b);
38 out[0..64].copy_from_slice(&b[0..64]);
39 self.dig.reset();
40 }
41}
42
43impl Digest for Blake3Digest512 {
44 fn new() -> Self {
45 Self {
46 dig: blake3::Hasher::new(),
47 }
48 }
49
50 fn new_with_prefix(data: impl AsRef<[u8]>) -> Self {
51 Self::new().chain_update(data)
52 }
53
54 fn chain_update(mut self, data: impl AsRef<[u8]>) -> Self
55 where
56 Self: Sized,
57 {
58 <Self as Update>::update(&mut self, data.as_ref());
59 self
60 }
61
62 fn finalize(self) -> Output<Self> {
63 let mut b = [0u8; 64];
64 self.dig.finalize_xof().fill(&mut b);
65 let mut out = GenericArray::<u8, U64>::default();
66 out[0..64].copy_from_slice(&b[0..64]);
67 out
68 }
69
70 fn finalize_reset(&mut self) -> Output<Self> {
71 let mut b = [0u8; 64];
72 self.dig.finalize_xof().fill(&mut b);
73 let mut out = GenericArray::<u8, U64>::default();
74 out[0..64].copy_from_slice(&b[0..64]);
75 self.dig.reset();
76 out
77 }
78
79 fn output_size() -> usize {
80 64
81 }
82
83 fn digest(data: impl AsRef<[u8]>) -> Output<Self> {
84 let mut dig = blake3::Hasher::new();
85 dig.update(data.as_ref());
86 let mut b = [0u8; 64];
87 dig.finalize_xof().fill(&mut b);
88 let mut out = GenericArray::<u8, U64>::default();
89 out[0..64].copy_from_slice(&b[0..64]);
90 out
91 }
92
93 fn update(&mut self, data: impl AsRef<[u8]>) {
94 <Self as Update>::update(self, data.as_ref())
95 }
96
97 fn finalize_into(self, out: &mut Output<Self>) {
98 <Self as FixedOutput>::finalize_into(self, out)
99 }
100
101 fn finalize_into_reset(&mut self, out: &mut Output<Self>)
102 where
103 Self: FixedOutputReset,
104 {
105 <Self as FixedOutputReset>::finalize_into_reset(self, out)
106 }
107
108 fn reset(&mut self)
109 where
110 Self: Reset,
111 {
112 <Self as Reset>::reset(self);
113 }
114}