Implement challenge 15 pkcs7 validation.
This commit is contained in:
16
src/bytes.rs
16
src/bytes.rs
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user