diff options
author | Jesse Luehrs <doy@tozt.net> | 2015-03-15 04:45:21 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2015-03-15 04:45:21 -0400 |
commit | 7312ac25a4a89c603cc8311f442a000433b5301b (patch) | |
tree | 190379c552d5ba7858696b7c69134a3522f0e142 | |
parent | 9d88163f2db433b9f31c98a8b3abac8eb17e9d4c (diff) | |
download | matasano-7312ac25a4a89c603cc8311f442a000433b5301b.tar.gz matasano-7312ac25a4a89c603cc8311f442a000433b5301b.zip |
problem 2
-rw-r--r-- | src/lib.rs | 12 | ||||
-rw-r--r-- | tests/lib.rs | 8 |
2 files changed, 19 insertions, 1 deletions
@@ -1,9 +1,19 @@ extern crate "rustc-serialize" as serialize; use serialize::base64::{ToBase64,STANDARD}; -use serialize::hex::FromHex; +use serialize::hex::{FromHex,ToHex}; pub fn hex_to_base64 (hex: &str) -> String { let bytes = hex.from_hex().unwrap(); return bytes.to_base64(STANDARD); } + +pub fn fixed_xor (str1: &str, str2: &str) -> String { + let bytes1 = str1.from_hex().unwrap(); + let bytes2 = str2.from_hex().unwrap(); + return bytes1.iter() + .zip(bytes2.iter()) + .map(|(&a, &b)| { a ^ b }) + .collect::<Vec<u8>>() + .to_hex(); +} diff --git a/tests/lib.rs b/tests/lib.rs index c2c79cc..0003696 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -6,3 +6,11 @@ fn problem_1 () { let base64 = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; assert_eq!(matasano::hex_to_base64(hex), base64); } + +#[test] +fn problem_2 () { + let str1 = "1c0111001f010100061a024b53535009181c"; + let str2 = "686974207468652062756c6c277320657965"; + let expected = "746865206b696420646f6e277420706c6179"; + assert_eq!(matasano::fixed_xor(str1, str2), expected); +} |