dstar_gateway_core/slowdata/
scrambler.rs1const SCRAMBLER_KEY: [u8; 3] = [0x70, 0x4F, 0x93];
8
9#[must_use]
11pub const fn descramble(bytes: [u8; 3]) -> [u8; 3] {
12 [
13 bytes[0] ^ SCRAMBLER_KEY[0],
14 bytes[1] ^ SCRAMBLER_KEY[1],
15 bytes[2] ^ SCRAMBLER_KEY[2],
16 ]
17}
18
19#[must_use]
23pub const fn scramble(bytes: [u8; 3]) -> [u8; 3] {
24 descramble(bytes)
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30
31 #[test]
32 fn descramble_then_scramble_roundtrips() {
33 for a in [0u8, 1, 0x42, 0x7F, 0x80, 0xFE, 0xFF] {
36 for b in [0u8, 1, 0x42, 0x7F, 0x80, 0xFE, 0xFF] {
37 for c in [0u8, 1, 0x42, 0x7F, 0x80, 0xFE, 0xFF] {
38 let input = [a, b, c];
39 assert_eq!(
40 scramble(descramble(input)),
41 input,
42 "roundtrip failed for {input:?}"
43 );
44 }
45 }
46 }
47 }
48
49 #[test]
50 fn descramble_zero_returns_key() {
51 assert_eq!(descramble([0u8; 3]), [0x70, 0x4F, 0x93]);
52 }
53
54 #[test]
55 fn scramble_zero_returns_key() {
56 assert_eq!(scramble([0u8; 3]), [0x70, 0x4F, 0x93]);
57 }
58
59 #[test]
60 fn scramble_key_returns_zero() {
61 assert_eq!(scramble([0x70, 0x4F, 0x93]), [0; 3]);
62 }
63}