commit 76497d355b64c76df041b4588830d3f2a207d322
parent 44264eeefe86f0d5ffe8088f9a96c2822b9745f9
Author: Jared Tobin <jared@jtobin.ca>
Date: Thu, 31 Aug 2017 13:55:43 +1200
Implement PKCS#7 padding in Rust.
Diffstat:
3 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/docs/s2.md b/docs/s2.md
@@ -6,9 +6,9 @@ PKCS #7 padding here just means that to pad a message of length 'l' to 'k'
bytes, one appends 'k - l' bytes -- each of value 'k - l' -- to the message.
So here we get four bytes' worth of padding, each of value 04:
- $ echo -n 'YELLOW SUBMARINE' | ./bin/pkcs 20 | xxd
+ $ echo -n 'YELLOW SUBMARINE' | ./bin/pkcs 20 | tr -d '\n' | xxd
00000000: 5945 4c4c 4f57 2053 5542 4d41 5249 4e45 YELLOW SUBMARINE
- 00000010: 0404 0404 0a .....
+ 00000010: 0404 0404 ....
#### 2.10
diff --git a/src/main.rs b/src/main.rs
@@ -4,6 +4,7 @@ mod s1c2;
mod s1c3;
mod s1c7;
+mod s2c9;
mod s2c10;
mod s2c11;
@@ -12,6 +13,7 @@ fn main() {
println!("s1c2:\n{}\n", s1c2::s1c2());
println!("s1c3:\n{}\n", s1c3::s1c3());
println!("s1c7:\n{}\n", s1c7::s1c7());
+ println!("s2c9:\n{}\n", s2c9::s2c9());
println!("s2c10:\n{}\n", s2c10::s2c10());
println!("s2c11:\n{}\n", s2c11::s2c11());
}
diff --git a/src/s2c9.rs b/src/s2c9.rs
@@ -0,0 +1,21 @@
+
+extern crate base64;
+
+pub fn pkcs(block: &[u8], size: usize) -> Vec<u8> {
+ let mut vec = Vec::with_capacity(size);
+ let len = block.len();
+
+ let padding_len = if len < size { (size - len) as u8 } else { 0 };
+ let padding = vec![padding_len; padding_len as usize ];
+
+ vec.extend_from_slice(block);
+ vec.extend_from_slice(&padding);
+ vec
+}
+
+pub fn s2c9() -> String {
+ let message = "YELLOW_SUBMARINE".as_bytes();
+ let padded = pkcs(message, 20);
+
+ base64::encode(&padded)
+}