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 {
|
pub fn xor(Bytes(a): &Bytes, Bytes(b): &Bytes) -> Bytes {
|
||||||
Bytes(
|
Bytes(
|
||||||
Iterator::zip(a.iter(), b.iter())
|
Iterator::zip(a.iter(), b.iter())
|
||||||
|
|||||||
@@ -21,4 +21,6 @@ fn main() {
|
|||||||
set2::challenge12();
|
set2::challenge12();
|
||||||
set2::challenge13();
|
set2::challenge13();
|
||||||
set2::challenge14();
|
set2::challenge14();
|
||||||
|
set2::challenge15();
|
||||||
|
set2::challenge16();
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/set2.rs
23
src/set2.rs
@@ -390,3 +390,26 @@ pub fn challenge14() {
|
|||||||
roundtrip_text.to_utf8()[..17].to_string()
|
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");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user