summaryrefslogtreecommitdiff
path: root/ext/node/ops/crypto/md5_sha1.rs
blob: 9164b0a1cbb8947b990534bbb89b5f19db287a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use core::fmt;
use digest::core_api::AlgorithmName;
use digest::core_api::BlockSizeUser;
use digest::core_api::Buffer;
use digest::core_api::BufferKindUser;
use digest::core_api::CoreWrapper;
use digest::core_api::FixedOutputCore;
use digest::core_api::OutputSizeUser;
use digest::core_api::UpdateCore;
use digest::HashMarker;
use digest::Output;
use digest::Reset;

pub type Md5Sha1 = CoreWrapper<Md5Sha1Core>;

pub struct Md5Sha1Core {
  md5: md5::Md5Core,
  sha1: sha1::Sha1Core,
}

impl HashMarker for Md5Sha1Core {}

impl BlockSizeUser for Md5Sha1Core {
  type BlockSize = sec1::consts::U64;
}

impl BufferKindUser for Md5Sha1Core {
  type BufferKind = digest::block_buffer::Eager;
}

impl OutputSizeUser for Md5Sha1Core {
  type OutputSize = sec1::consts::U36;
}

impl UpdateCore for Md5Sha1Core {
  #[inline]
  fn update_blocks(&mut self, blocks: &[digest::core_api::Block<Self>]) {
    self.md5.update_blocks(blocks);
    self.sha1.update_blocks(blocks);
  }
}

impl FixedOutputCore for Md5Sha1Core {
  #[inline]
  fn finalize_fixed_core(
    &mut self,
    buffer: &mut Buffer<Self>,
    out: &mut Output<Self>,
  ) {
    let mut md5_output = Output::<md5::Md5Core>::default();
    self
      .md5
      .finalize_fixed_core(&mut buffer.clone(), &mut md5_output);
    let mut sha1_output = Output::<sha1::Sha1Core>::default();
    self.sha1.finalize_fixed_core(buffer, &mut sha1_output);
    out[..16].copy_from_slice(&md5_output);
    out[16..].copy_from_slice(&sha1_output);
  }
}

impl Default for Md5Sha1Core {
  #[inline]
  fn default() -> Self {
    Self {
      md5: Default::default(),
      sha1: Default::default(),
    }
  }
}

impl Clone for Md5Sha1Core {
  #[inline]
  fn clone(&self) -> Self {
    Self {
      md5: self.md5.clone(),
      sha1: self.sha1.clone(),
    }
  }
}

impl Reset for Md5Sha1Core {
  #[inline]
  fn reset(&mut self) {
    *self = Default::default();
  }
}

impl AlgorithmName for Md5Sha1Core {
  fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.write_str("Md5Sha1")
  }
}

impl fmt::Debug for Md5Sha1Core {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.write_str("Md5Sha1Core { ... }")
  }
}