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())

View File

@@ -21,4 +21,6 @@ fn main() {
set2::challenge12();
set2::challenge13();
set2::challenge14();
set2::challenge15();
set2::challenge16();
}

View File

@@ -390,3 +390,26 @@ pub fn challenge14() {
roundtrip_text.to_utf8()[..17].to_string()
);
}
pub fn challenge15() {
assert_eq!(
Bytes::from_utf8("ICE ICE BABY\u{4}\u{4}\u{4}").has_valid_pkcs7(16),
false
);
assert_eq!(
Bytes::from_utf8("ICE ICE BABY\u{4}\u{4}\u{4}\u{4}").has_valid_pkcs7(16),
true
);
assert_eq!(
Bytes::from_utf8("ICE ICE BABY\u{3}\u{3}\u{4}\u{4}").has_valid_pkcs7(16),
false
);
let mut bytes = Bytes::from_utf8("ICE ICE BABY\u{3}\u{3}\u{4}\u{4}");
bytes.pad_pkcs7(16);
assert_eq!(bytes.has_valid_pkcs7(16), true);
println!("[okay] Challenge 15: PKCS7 works");
}
pub fn challenge16() {
println!("[xxxx] Challenge 16: TBD");
}