Implement challenge 15 pkcs7 validation.

This commit is contained in:
2022-06-28 13:28:12 -04:00
parent f6b4c98826
commit fd6f9464cc
3 changed files with 41 additions and 0 deletions

View File

@@ -101,6 +101,22 @@ impl Bytes {
}
}
pub fn has_valid_pkcs7(&mut self, block_size: usize) -> bool {
if self.len() > 0 && self.len() % block_size != 0 {
return false;
}
let last_block_index = self.len() / block_size - 1;
let last_block = self.get_block(last_block_index, block_size).0;
let pad_byte_count = last_block[block_size - 1];
for i in 0..(pad_byte_count as usize) {
let byte = last_block[block_size - 1 - i];
if byte != pad_byte_count {
return false;
}
}
return true;
}
pub fn xor(Bytes(a): &Bytes, Bytes(b): &Bytes) -> Bytes {
Bytes(
Iterator::zip(a.iter(), b.iter())