move common stuff to common crate

This commit is contained in:
Thomas Lindner 2023-12-16 21:46:14 +01:00
parent 8476e7f6b1
commit 41581053dd
12 changed files with 323 additions and 133 deletions

291
Cargo.lock generated
View file

@ -4,58 +4,96 @@ version = 3
[[package]]
name = "aho-corasick"
version = "0.5.3"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
dependencies = [
"memchr",
]
[[package]]
name = "bitflags"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]]
name = "common"
version = "0.1.0"
[[package]]
name = "detect_xor"
version = "0.1.0"
dependencies = [
"common",
"env_logger",
"log 0.3.9",
"log",
"rustc-serialize",
]
[[package]]
name = "env_logger"
version = "0.3.5"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f"
checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece"
dependencies = [
"log 0.3.9",
"humantime",
"is-terminal",
"log",
"regex",
"termcolor",
]
[[package]]
name = "errno"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "fixed_xor"
version = "0.1.0"
dependencies = [
"common",
"env_logger",
"log 0.3.9",
"log",
"rustc-serialize",
]
[[package]]
name = "hermit-abi"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "hex2base64"
version = "0.1.0"
dependencies = [
"env_logger",
"log 0.3.9",
"log",
"rustc-serialize",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "is-terminal"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
dependencies = [
"winapi",
"winapi-build",
"hermit-abi",
"rustix",
"windows-sys 0.48.0",
]
[[package]]
@ -65,13 +103,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
[[package]]
name = "log"
version = "0.3.9"
name = "linux-raw-sys"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
dependencies = [
"log 0.4.20",
]
checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]]
name = "log"
@ -81,31 +116,38 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "memchr"
version = "0.1.11"
version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
dependencies = [
"libc",
]
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "regex"
version = "0.1.80"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
"utf8-ranges",
]
[[package]]
name = "regex-syntax"
version = "0.3.9"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rustc-serialize"
@ -114,47 +156,196 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe834bc780604f4674073badbad26d7219cadfb4a2275802db12cbae17498401"
[[package]]
name = "thread-id"
version = "2.0.0"
name = "rustix"
version = "0.38.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316"
dependencies = [
"kernel32-sys",
"bitflags",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.52.0",
]
[[package]]
name = "thread_local"
version = "0.2.7"
name = "termcolor"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449"
dependencies = [
"thread-id",
"winapi-util",
]
[[package]]
name = "utf8-ranges"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
[[package]]
name = "winapi"
version = "0.2.8"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-build"
version = "0.1.1"
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "xor_cipher"
version = "0.1.0"
dependencies = [
"common",
"env_logger",
"log 0.3.9",
"log",
"rustc-serialize",
]

View file

@ -1,8 +1,10 @@
[workspace]
members = [
"common",
"set1/challenge1_hex2base64",
"set1/challenge2_fixed_xor",
"set1/challenge3_xor_cipher",
"set1/challenge4_detect_xor",
]
resolver = "2"

7
common/Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "common"
version = "0.1.0"
edition = "2021"
authors = ["Thomas Lindner <tom@dl6tom.de>"]
[dependencies]

37
common/src/lib.rs Normal file
View file

@ -0,0 +1,37 @@
use std::iter::repeat;
pub fn score(plaintext: &Vec<u8>) -> u32 {
let ranking = [24, 7, 15, 17, 26, 11, 10, 19, 22, 4, 5, 16, 13,
21, 23, 8, 2, 18, 20, 25, 14, 6, 12, 3, 9, 1];
let mut score = 0;
for ch in plaintext.iter() {
if ch >= &97 && ch <= &122 {
score += ranking[(ch - 97) as usize];
}
}
score
}
pub fn fixed_xor_cipher(ciphertext: &Vec<u8>, keyiter: &mut dyn Iterator<Item = &u8>) -> Vec<u8> {
ciphertext.iter().zip(keyiter).map(|(c, k)| c ^ k).collect()
}
pub fn xor_cipher(ciphertext: &Vec<u8>, key: u8) -> Vec<u8> {
fixed_xor_cipher(ciphertext, &mut repeat(&key))
}
pub fn break_xor_cipher(ciphertext: &Vec<u8>) -> (Vec<u8>, u32) {
let mut maxscore = 0;
let mut decrypted: Vec<u8> = vec![];
for key in 0..0xff {
let plaintext: Vec<u8> = xor_cipher(&ciphertext, key);
let s = score(&plaintext);
if s > maxscore {
maxscore = s;
decrypted = plaintext;
}
}
(decrypted, maxscore)
}

View file

@ -1,9 +1,10 @@
[package]
name = "hex2base64"
version = "0.1.0"
authors = ["Thomas Lindner <thomas.lindner@fau.de>"]
edition = "2021"
authors = ["Thomas Lindner <tom@dl6tom.de>"]
[dependencies]
log = "0.3.6"
env_logger = "0.3.4"
log = "0.4"
env_logger = "0.10"
rustc-serialize = "0.3"

View file

@ -1,14 +1,10 @@
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate rustc_serialize;
use log::error;
use rustc_serialize::hex::FromHex;
use rustc_serialize::base64::{STANDARD, ToBase64};
use std::io::{BufRead, stdin};
fn main() {
env_logger::init().unwrap();
env_logger::init();
let stdin = stdin();
for line in stdin.lock().lines().filter_map(|x| x.ok()) {

View file

@ -1,9 +1,11 @@
[package]
name = "fixed_xor"
version = "0.1.0"
authors = ["Thomas Lindner <thomas.lindner@fau.de>"]
edition = "2021"
authors = ["Thomas Lindner <tom@dl6tom.de>"]
[dependencies]
log = "0.3.6"
env_logger = "0.3.4"
common = { path = "../../common" }
log = "0.4"
env_logger = "0.10"
rustc-serialize = "0.3"

View file

@ -1,8 +1,5 @@
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate rustc_serialize;
use common::fixed_xor_cipher;
use log::error;
use rustc_serialize::hex::{FromHex, ToHex};
use std::io::{BufRead, stdin};
@ -21,12 +18,12 @@ impl<T> Iterator for Pairwise<T> where T: Iterator {
}
fn main() {
env_logger::init().unwrap();
env_logger::init();
let stdin = stdin();
for (line1, line2) in Pairwise(stdin.lock().lines().filter_map(|x| x.ok())) {
match (line1.from_hex(), line2.from_hex()) {
(Ok(v1), Ok(v2)) => println!("{}", v1.iter().zip(v2).map(|(a, b)| a ^ b).collect::<Vec<u8>>().to_hex()),
(Ok(v1), Ok(v2)) => println!("{}", fixed_xor_cipher(&v1, &mut v2.iter()).to_hex()),
(Err(e), _) => error!("{}", e),
(_, Err(e)) => error!("{}", e)
}

View file

@ -1,9 +1,11 @@
[package]
name = "xor_cipher"
version = "0.1.0"
authors = ["Thomas Lindner <thomas.lindner@fau.de>"]
edition = "2021"
authors = ["Thomas Lindner <tom@dl6tom.de>"]
[dependencies]
log = "0.3.6"
env_logger = "0.3.4"
common = { path = "../../common" }
log = "0.4"
env_logger = "0.10"
rustc-serialize = "0.3"

View file

@ -1,43 +1,16 @@
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate rustc_serialize;
use common::break_xor_cipher;
use log::error;
use rustc_serialize::hex::FromHex;
use std::io::{BufRead, stdin};
use std::iter::repeat;
fn score(plaintext: &Vec<u8>) -> u32 {
let ranking = [24, 7, 15, 17, 26, 11, 10, 19, 22, 4, 5, 16, 13,
21, 23, 8, 2, 18, 20, 25, 14, 6, 12, 3, 9, 1];
let mut score = 0;
for ch in plaintext.iter() {
if ch >= &97 && ch <= &122 {
score += ranking[(ch - 97) as usize];
}
}
score
}
fn main() {
env_logger::init().unwrap();
env_logger::init();
let stdin = stdin();
for line in stdin.lock().lines().filter_map(|x| x.ok()) {
match line.from_hex() {
Ok(ciphertext) => {
let mut maxscore = 0;
let mut decrypted: Vec<u8> = vec![];
for key in 0..0xff {
let plaintext: Vec<u8> = ciphertext.iter().zip(repeat(key)).map(|(c, k)| c ^ k).collect();
let s = score(&plaintext);
if s > maxscore {
maxscore = s;
decrypted = plaintext;
}
}
let (decrypted, _) = break_xor_cipher(&ciphertext);
match String::from_utf8(decrypted) {
Ok(s) => println!("{}", s),
Err(e) => error!("{}", e)

View file

@ -1,9 +1,11 @@
[package]
name = "detect_xor"
version = "0.1.0"
authors = ["Thomas Lindner <thomas.lindner@fau.de>"]
edition = "2021"
authors = ["Thomas Lindner <tom@dl6tom.de>"]
[dependencies]
log = "0.3.6"
env_logger = "0.3.4"
common = { path = "../../common" }
log = "0.4"
env_logger = "0.10"
rustc-serialize = "0.3"

View file

@ -1,27 +1,10 @@
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate rustc_serialize;
use common::break_xor_cipher;
use log::error;
use rustc_serialize::hex::FromHex;
use std::io::{BufRead, stdin};
use std::iter::repeat;
fn score(plaintext: &Vec<u8>) -> u32 {
let ranking = [24, 7, 15, 17, 26, 11, 10, 19, 22, 4, 5, 16, 13,
21, 23, 8, 2, 18, 20, 25, 14, 6, 12, 3, 9, 1];
let mut score = 0;
for ch in plaintext.iter() {
if ch >= &97 && ch <= &122 {
score += ranking[(ch - 97) as usize];
}
}
score
}
fn main() {
env_logger::init().unwrap();
env_logger::init();
let stdin = stdin();
let mut maxscore = 0;
let mut decrypted: Vec<u8> = vec![];
@ -29,13 +12,10 @@ fn main() {
for line in stdin.lock().lines().filter_map(|x| x.ok()) {
match line.from_hex() {
Ok(ciphertext) => {
for key in 0..0xff {
let plaintext: Vec<u8> = ciphertext.iter().zip(repeat(key)).map(|(c, k)| c ^ k).collect();
let s = score(&plaintext);
if s > maxscore {
maxscore = s;
decrypted = plaintext;
}
let (plaintext, score) = break_xor_cipher(&ciphertext);
if score > maxscore {
maxscore = score;
decrypted = plaintext;
}
}
Err(e) => error!("{}", e)