Skip to content

Commit

Permalink
feat: add pass_bits parameter to scramble()
Browse files Browse the repository at this point in the history
  • Loading branch information
WieeRd committed May 13, 2024
1 parent 27dcb55 commit 02d47e1
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/scramble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ impl<E: Encrypter> Scrambler<E> {
Self { encrypter, padding }
}

pub fn scramble(&self, bytes: &[u8; 16], n_bits: usize) -> [u8; 16] {
pub fn scramble(&self, bytes: &[u8; 16], n_bits: usize, pass_bits: usize) -> [u8; 16] {
if n_bits > 128 {
panic!("`n_bits` should be less than 128");
}

let mut result: [u8; 16] = [0; 16];
for i in 0..n_bits {
for i in pass_bits..n_bits {
// first `i` bits from `bytes`, the rest from `padding`
// padded = (bytes & !mask) | (self.padding & mask)
let padded = {
Expand Down Expand Up @@ -112,15 +112,17 @@ impl<E: Encrypter> Scrambler<E> {
let mut bytes = [0; 16];
bytes[..4].copy_from_slice(&addr.octets());

let anonymized = self.scramble(&bytes, 32);
// FEAT: ASAP: calculate pass_bits based on ip class
// match bytes[0] {}
let anonymized = self.scramble(&bytes, 32, 0);
let truncated: [u8; 4] = anonymized[..4].try_into().unwrap();

truncated.into()
}

pub fn scramble_ipv6(&self, addr: Ipv6Addr) -> Ipv6Addr {
let bytes = addr.octets();
self.scramble(&bytes, 128).into()
self.scramble(&bytes, 128, 0).into()
}
}

Expand Down

0 comments on commit 02d47e1

Please sign in to comment.