fn text_to_chars(text: &str) -> Vec<u32> {
text.chars().map(|c| c as u32).collect()
}
fn apply_salt_to_char(code: u32, salt: &str) -> u32 {
let salt_chars = text_to_chars(salt);
salt_chars.iter().fold(code, |acc, &b| acc ^ b)
}
pub fn hide(salt: &str, text: &str) -> String {
fn byte_hex(n: u32) -> String {
format!("{:02x}", n)
}
let mut encoded = String::new();
for c in text.chars() {
let code = c as u32;
let code = apply_salt_to_char(code, salt);
encoded += &byte_hex(code);
}
return encoded
}
pub fn unhide(salt: &str, encoded: &str) -> String {
let mut decoded = "".to_string();
for hex in encoded.as_bytes().chunks(2) {
let code = u32::from_str_radix(std::str::from_utf8(hex).unwrap(), 16).unwrap();
let code = apply_salt_to_char(code, salt);
decoded += &std::char::from_u32(code).unwrap().to_string();
}
return decoded
}