cryptopals

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit e2834bcb33a4d98a7aa33c2bcbb2dc406fffeadc
Author: minerva-jupiter <ryouturn@gmail.com>
Date:   Wed, 25 Mar 2026 23:48:19 +0900

init

Diffstat:
A.gitignore | 1+
ACargo.lock | 7+++++++
ACargo.toml | 6++++++
Asrc/bin/1-1.rs | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/bin/1-2.rs | 11+++++++++++
Asrc/lib.rs | 9+++++++++
Asrc/main.rs | 3+++
7 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "cryptopals" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "cryptopals" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/bin/1-1.rs b/src/bin/1-1.rs @@ -0,0 +1,51 @@ +use cryptopals::utils::read_buffer; + +fn main() -> Result<(), Box<dyn std::error::Error>> { + let hex_input = read_buffer()?; + let bytes = hex_to_bytes(&hex_input); + let base64_output = bytes_to_base64(&bytes); + + println!("{}", base64_output); + + Ok(()) +} + +const BASE64_TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +fn hex_to_bytes(hex: &str) -> Vec<u8> { + hex.as_bytes() + .chunks(2) + .map(|chunk| { + let s = std::str::from_utf8(chunk).unwrap(); + u8::from_str_radix(s, 16).expect("Invalid hex") + }) + .collect() +} + +fn bytes_to_base64(bytes: &[u8]) -> String { + let mut result = String::with_capacity((bytes.len() + 2) / 3 * 4); + + for chunk in bytes.chunks(3) { + let b0 = chunk[0] as usize; + let b1 = chunk.get(1).map(|&b| b as usize).unwrap_or(0); + let b2 = chunk.get(2).map(|&b| b as usize).unwrap_or(0); + + let n = (b0 << 16) | (b1 << 8) | b2; + + result.push(BASE64_TABLE[(n >> 18) & 0x3F] as char); + result.push(BASE64_TABLE[(n >> 12) & 0x3F] as char); + + if chunk.len() > 1 { + result.push(BASE64_TABLE[(n >> 6) & 0x3F] as char); + } else { + result.push('='); + } + + if chunk.len() > 2 { + result.push(BASE64_TABLE[n & 0x3F] as char); + } else { + result.push('='); + } + } + result +} diff --git a/src/bin/1-2.rs b/src/bin/1-2.rs @@ -0,0 +1,11 @@ +fn main() -> Result<(), Box<dyn std::error::Error>> { + println!("Enter first buffer"); + let first = std::read_line()?; + println!("Enter second buffer"); + let second = std::read_line()?; + if first.len() != second.len() { + println!("buffer length is not matched!"); + return Err(Box::new(()) as Box<dyn std::error::Error>); + } + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs @@ -0,0 +1,9 @@ +pub mod utils { + pub fn read_buffer() -> Result<String, Box<dyn std::error::Error>> { + let mut buffer = String::new(); + std::io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line."); + Ok(buffer.trim().to_string()) + } +} diff --git a/src/main.rs b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}