Finish challenge 16 and problem set 2.
This commit is contained in:
19
src/bytes.rs
19
src/bytes.rs
@@ -1,6 +1,6 @@
|
||||
#![allow(dead_code)]
|
||||
use rand::Rng;
|
||||
use std::fmt::Write; // need to import this trait
|
||||
use std::fmt::Write;
|
||||
|
||||
#[derive(PartialEq, PartialOrd, Debug)]
|
||||
pub struct Bytes(pub Vec<u8>);
|
||||
@@ -117,6 +117,23 @@ impl Bytes {
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn remove_pkcs7(&mut self, block_size: usize) -> () {
|
||||
if !self.has_valid_pkcs7(block_size) {
|
||||
return;
|
||||
}
|
||||
let Bytes(v) = self;
|
||||
let pad_byte_count = v[v.len() - 1];
|
||||
for _ in 0..(pad_byte_count as usize) {
|
||||
v.pop();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn flip_bit(&mut self, byte_index: usize, bit_index: usize) -> () {
|
||||
let Bytes(v) = self;
|
||||
let flip_mask: u8 = 0b1 << bit_index;
|
||||
v[byte_index] ^= flip_mask;
|
||||
}
|
||||
|
||||
pub fn xor(Bytes(a): &Bytes, Bytes(b): &Bytes) -> Bytes {
|
||||
Bytes(
|
||||
Iterator::zip(a.iter(), b.iter())
|
||||
|
||||
Reference in New Issue
Block a user