cloudillo_types/
hasher.rs1use base64::Engine;
7use sha2::{Digest, Sha256};
8
9pub enum Hasher {
10 V1(Sha256),
11}
12
13impl Default for Hasher {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl Hasher {
20 pub fn new() -> Self {
21 Self::V1(Sha256::new())
22 }
23
24 pub fn new_v1() -> Self {
25 Self::V1(Sha256::new())
26 }
27
28 pub fn update(&mut self, data: &[u8]) {
29 match self {
30 Self::V1(hasher) => hasher.update(data),
31 }
32 }
33
34 pub fn finalize(self, prefix: &str) -> String {
35 match self {
36 Self::V1(hasher) => {
38 prefix.to_string()
39 + "1~" + &base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(hasher.finalize())
40 }
41 }
42 }
43}
44
45pub fn hash_v1(prefix: &str, data: &[u8]) -> Box<str> {
46 let mut hasher = Hasher::new();
47 hasher.update(data);
48 let result = hasher.finalize(prefix);
49
50 result.into()
51}
52
53pub fn hash(prefix: &str, data: &[u8]) -> Box<str> {
54 hash_v1(prefix, data)
55}
56
57