key_vault/codex/identity.rs
1//! The no-op codex.
2
3use super::Codex;
4
5/// Codex that leaves every byte unchanged.
6///
7/// `IdentityCodex` is the default Layer 5 implementation. It satisfies the
8/// involution requirement trivially (`x → x → x`) and costs nothing at runtime.
9/// Use a non-identity codex when you want the Layer 5 defense; use this one
10/// when you are deliberately turning Layer 5 off.
11///
12/// # Examples
13///
14/// ```
15/// use key_vault::codex::{Codex, IdentityCodex};
16///
17/// let c = IdentityCodex;
18/// assert_eq!(c.encode(0xab), 0xab);
19/// assert_eq!(c.decode(0xab), 0xab);
20/// ```
21#[derive(Debug, Default, Clone, Copy)]
22pub struct IdentityCodex;
23
24impl Codex for IdentityCodex {
25 #[inline]
26 fn encode(&self, byte: u8) -> u8 {
27 byte
28 }
29
30 #[inline]
31 fn decode(&self, byte: u8) -> u8 {
32 byte
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn passes_every_byte_through() {
42 let c = IdentityCodex;
43 for b in 0u8..=255 {
44 assert_eq!(c.encode(b), b);
45 assert_eq!(c.decode(b), b);
46 }
47 }
48
49 #[test]
50 fn involution_holds_for_every_byte() {
51 let c = IdentityCodex;
52 for b in 0u8..=255 {
53 assert_eq!(c.decode(c.encode(b)), b);
54 }
55 }
56}