use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("{0}")]
pub struct HexError(pub String);
pub fn decrypt(cipher: &[u8], mut r: u16) -> (Vec<u8>, u16) {
let mut plain = Vec::with_capacity(cipher.len());
for &c in cipher {
plain.push(c ^ (r >> 8) as u8);
r = next_key(c, r);
}
(plain, r)
}
pub fn encrypt(plain: &[u8], mut r: u16) -> (Vec<u8>, u16) {
let mut cipher = Vec::with_capacity(plain.len());
for &p in plain {
let c = p ^ (r >> 8) as u8;
cipher.push(c);
r = next_key(c, r);
}
(cipher, r)
}
fn next_key(cipher: u8, r: u16) -> u16 {
((cipher as u32 + r as u32)
.wrapping_mul(52845)
.wrapping_add(22719)
& 0xFFFF) as u16
}
pub fn hex_string(s: &[u8]) -> Vec<u8> {
const DIGITS: &[u8; 16] = b"0123456789abcdef";
let mut out = Vec::with_capacity(s.len() * 2);
for &b in s {
out.push(DIGITS[(b >> 4) as usize]);
out.push(DIGITS[(b & 0xF) as usize]);
}
out
}
pub fn de_hex_string(h: &[u8]) -> Result<Vec<u8>, HexError> {
let digits: Vec<u8> = h.iter().copied().filter(|b| !is_ascii_space(*b)).collect();
unhexlify(&digits)
}
pub fn is_ascii_space(b: u8) -> bool {
matches!(b, b' ' | b'\t' | b'\n' | b'\r' | 0x0B | 0x0C)
}
pub fn unhexlify(digits: &[u8]) -> Result<Vec<u8>, HexError> {
if !digits.len().is_multiple_of(2) {
return Err(HexError("Odd-length string".into()));
}
let value = |b: u8| -> Result<u8, HexError> {
(b as char)
.to_digit(16)
.map(|d| d as u8)
.ok_or_else(|| HexError("Non-hexadecimal digit found".into()))
};
digits
.chunks(2)
.map(|pair| Ok(value(pair[0])? << 4 | value(pair[1])?))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hex_round_trip() {
assert_eq!(hex_string(b"\x01\xab"), b"01ab");
assert_eq!(de_hex_string(b"01 A\nb"), Ok(b"\x01\xab".to_vec()));
assert!(de_hex_string(b"0").is_err());
assert!(de_hex_string(b"0g").is_err());
}
}