seedelf_cli/hashing.rs
1use blake2::Blake2bVar;
2use blake2::digest::core_api::RtVariableCoreWrapper;
3use blake2::digest::{Update, VariableOutput};
4use hex;
5use sha3::{Digest, Sha3_256};
6
7/// Computes the BLAKE2b-224 hash of the input data.
8///
9/// This function accepts a string input, which can be a hex-encoded string or a raw string.
10/// If the input is hex-encoded, it is decoded to bytes. Otherwise, the raw bytes of the string are used.
11/// The resulting hash is 224 bits (28 bytes) and is returned as a hex-encoded string.
12///
13/// # Arguments
14///
15/// * `data` - A string slice representing the input data. It can be a hex-encoded string or plain text.
16///
17/// # Returns
18///
19/// * `String` - The BLAKE2b-224 hash of the input data, encoded as a hex string.
20///
21/// # Panics
22///
23/// * If creating or finalizing the BLAKE2b hasher fails.
24/// * If the input hex string cannot be decoded.
25pub fn blake2b_224(data: &str) -> String {
26 // Decode hex string to bytes if needed
27 let decoded_data: Vec<u8> = if let Ok(decoded) = hex::decode(data) {
28 decoded
29 } else {
30 data.as_bytes().to_vec()
31 };
32
33 // Create a BLAKE2b hasher with a 224-bit output
34 let mut hasher: RtVariableCoreWrapper<blake2::Blake2bVarCore> =
35 Blake2bVar::new(28).expect("Failed to create BLAKE2b hasher");
36 hasher.update(&decoded_data);
37
38 // Retrieve the hash result
39 let mut result: [u8; 28] = [0u8; 28];
40 hasher
41 .finalize_variable(&mut result)
42 .expect("Failed to finalize hash");
43
44 // Convert to hex string
45 hex::encode(result)
46}
47
48/// Computes the BLAKE2b-256 hash of the input data.
49///
50/// This function accepts a string input, which can be a hex-encoded string or a raw string.
51/// If the input is hex-encoded, it is decoded to bytes. Otherwise, the raw bytes of the string are used.
52/// The resulting hash is 256 bits (32 bytes) and is returned as a hex-encoded string.
53///
54/// # Arguments
55///
56/// * `data` - A string slice representing the input data. It can be a hex-encoded string or plain text.
57///
58/// # Returns
59///
60/// * `String` - The BLAKE2b-256 hash of the input data, encoded as a hex string.
61///
62/// # Panics
63///
64/// * If creating or finalizing the BLAKE2b hasher fails.
65/// * If the input hex string cannot be decoded.
66pub fn blake2b_256(data: &str) -> String {
67 // Decode hex string to bytes if needed
68 let decoded_data: Vec<u8> = if let Ok(decoded) = hex::decode(data) {
69 decoded
70 } else {
71 data.as_bytes().to_vec()
72 };
73
74 // Create a BLAKE2b hasher with a 256-bit output
75 let mut hasher: RtVariableCoreWrapper<blake2::Blake2bVarCore> =
76 Blake2bVar::new(32).expect("Failed to create BLAKE2b hasher");
77 hasher.update(&decoded_data);
78
79 // Retrieve the hash result
80 let mut result: [u8; 32] = [0u8; 32];
81 hasher
82 .finalize_variable(&mut result)
83 .expect("Failed to finalize hash");
84
85 // Convert to hex string
86 hex::encode(result)
87}
88
89/// Computes the SHA3-256 hash of the input data.
90///
91/// This function accepts a string input, which is expected to be hex-encoded.
92/// If the input is not a valid hex string, it falls back to hashing an empty byte array.
93/// The resulting hash is 256 bits (32 bytes) and is returned as a hex-encoded string.
94///
95/// # Arguments
96///
97/// * `data` - A string slice representing the input data, expected to be hex-encoded.
98///
99/// # Returns
100///
101/// * `String` - The SHA3-256 hash of the input data, encoded as a hex string.
102///
103/// # Panics
104///
105/// * This function will not panic, but if `data` is not a valid hex string,
106/// it will hash an empty byte array.
107pub fn sha3_256(data: &str) -> String {
108 let mut sha3_hasher = Sha3_256::new();
109 Digest::update(&mut sha3_hasher, hex::decode(data).unwrap_or_default());
110 // Retrieve the hash result
111 let result = sha3_hasher.finalize();
112
113 // Convert to hex string
114 hex::encode(result)
115}