Skip to main content

quick_start/
quick_start.rs

1use anyhow::Result;
2use colored::*;
3
4// Import key modules from ruscrypt
5use ruscrypt::classical::caesar;
6use ruscrypt::stream::rc4;
7use ruscrypt::block::aes;
8use ruscrypt::asym::rsa;
9use ruscrypt::hash::sha256;
10
11fn main() -> Result<()> {
12    print_quick_start_banner();
13    
14    println!("{}", "Let's explore RusCrypt with 5 essential examples!\n".bright_blue());
15    
16    // Classical cipher example
17    println!("{}", "1. πŸ“œ Classical Cryptography (Caesar Cipher)".cyan().bold());
18    quick_classical_example()?;
19    
20    // Stream cipher example
21    println!("\n{}", "2. 🌊 Stream Encryption (RC4)".green().bold());
22    quick_stream_example()?;
23    
24    // Block cipher example
25    println!("\n{}", "3. πŸ›‘οΈ  Modern Encryption (AES)".blue().bold());
26    quick_block_example()?;
27    
28    // Asymmetric encryption example
29    println!("\n{}", "4. πŸ” Public-Key Cryptography (RSA)".purple().bold());
30    quick_asymmetric_example()?;
31    
32    // Hash function example
33    println!("\n{}", "5. πŸ”’ Secure Hashing (SHA-256)".magenta().bold());
34    quick_hash_example()?;
35    
36    print_next_steps();
37    
38    Ok(())
39}
40
41fn print_quick_start_banner() {
42    println!("{}", r"
43  ____            ____                  _   
44 |  _ \ _   _ ___ / ___|_ __ _   _ _ __ | |_ 
45 | |_) | | | / __| |   | '__| | | | '_ \| __|
46 |  _ <| |_| \__ \ |___| |  | |_| | |_) | |_ 
47 |_| \_\\__,_|___/\____|_|   \__, | .__/ \__|
48                             |___/|_|        
49    ".bright_blue());
50    
51    println!("{}", "πŸš€ RusCrypt Quick Start Guide".bright_blue().bold());
52    println!("{}", "Master cryptography in 5 simple examples".cyan().italic());
53    println!();
54}
55
56fn quick_classical_example() -> Result<()> {
57    println!("{}", "   Historical cipher used by Julius Caesar.".white());
58    println!("{}", "   πŸŽ“ Educational: Shows basic substitution concepts".yellow());
59    
60    let message = "HELLO WORLD";
61    let shift = 3;
62    
63    // Encrypt
64    let encrypted = caesar::encrypt(message, shift)?;
65    println!("   πŸ“ Original: {}", message.cyan());
66    println!("   πŸ”’ Shift: {} positions", shift.to_string().yellow());
67    println!("   πŸ”’ Encrypted: {} β†’ {}", message.white(), encrypted.green().bold());
68    
69    // Decrypt
70    let decrypted = caesar::decrypt(&encrypted, shift)?;
71    println!("   πŸ”“ Decrypted: {} β†’ {}", encrypted.white(), decrypted.blue().bold());
72    
73    println!("   ⚠️  Security: Educational only - easily broken!");
74    println!("   πŸ’‘ Try: {}", "cargo run -- encrypt --caesar".bright_green());
75    
76    Ok(())
77}
78
79fn quick_stream_example() -> Result<()> {
80    println!("{}", "   Encrypts data byte-by-byte using a keystream.".white());
81    println!("{}", "   ⚠️  Deprecated: Known vulnerabilities, educational use only".yellow());
82    
83    let message = "Secret message!";
84    let key = "mykey123";
85    
86    // Encrypt
87    let encrypted = rc4::encrypt(message, key, "base64")?;
88    println!("   πŸ“ Original: {}", message.cyan());
89    println!("   πŸ—οΈ  Key: {}", key.yellow());
90    println!("   πŸ”’ Encrypted: {}", encrypted.green().bold());
91    
92    // Decrypt
93    let decrypted = rc4::decrypt(&encrypted, key, "base64")?;
94    println!("   πŸ”“ Decrypted: {}", decrypted.blue().bold());
95    
96    println!("   πŸ”„ Round-trip: {} = {}", message == decrypted, if message == decrypted { "βœ…" } else { "❌" });
97    println!("   πŸ’‘ Try: {}", "cargo run -- encrypt --rc4".bright_green());
98    
99    Ok(())
100}
101
102fn quick_block_example() -> Result<()> {
103    println!("{}", "   Industry-standard symmetric encryption.".white());
104    println!("{}", "   βœ… Secure: Recommended for modern applications".green());
105    
106    let message = "Top secret data!";
107    let password = "strongpassword";
108    
109    // Encrypt with AES-256 CBC
110    let encrypted = aes::encrypt(message, password, "256", "CBC", "base64")?;
111    println!("   πŸ“ Original: {}", message.cyan());
112    println!("   πŸ”‘ Password: {}", password.yellow());
113    println!("   πŸ”’ Encrypted (AES-256 CBC): {}", encrypted.green().bold());
114    
115    // Decrypt
116    let decrypted = aes::decrypt(&encrypted, password, "256", "CBC", "base64")?;
117    println!("   πŸ”“ Decrypted: {}", decrypted.blue().bold());
118    
119    println!("   πŸ›‘οΈ  Security: Bank-grade encryption!");
120    println!("   πŸ’‘ Try: {}", "cargo run -- encrypt --aes".bright_green());
121    
122    Ok(())
123}
124
125fn quick_asymmetric_example() -> Result<()> {
126    println!("{}", "   Public-key cryptography for secure communication.".white());
127    println!("{}", "   πŸ” Concept: Different keys for encryption/decryption".yellow());
128    
129    let message = "Hello RSA!";
130    
131    // Encrypt (generates key pair automatically)
132    let (encrypted, private_key) = rsa::encrypt(message, "512", "base64", "n:e")?;
133    println!("   πŸ“ Original: {}", message.cyan());
134    println!("   πŸ”’ Encrypted: {}...", encrypted[..30].green().bold());
135    println!("   πŸ”‘ Private Key: {}...", private_key[..20].yellow());
136    
137    // Decrypt
138    let decrypted = rsa::decrypt(&encrypted, &private_key, "base64")?;
139    println!("   πŸ”“ Decrypted: {}", decrypted.blue().bold());
140    
141    println!("   🌐 Use case: Secure communication without shared secrets");
142    println!("   πŸ’‘ Try: {}", "cargo run -- encrypt --rsa".bright_green());
143    
144    Ok(())
145}
146
147fn quick_hash_example() -> Result<()> {
148    println!("{}", "   Creates unique fingerprints for any data.".white());
149    println!("{}", "   βœ… Secure: Perfect for data integrity and passwords".green());
150    
151    let messages = vec!["Hello", "Hello!", "hello"];
152    
153    for (i, message) in messages.iter().enumerate() {
154        let hash = sha256::hash(message)?;
155        println!("   πŸ“ Input {}: {} β†’ Hash: {}...", 
156                (i + 1).to_string().white(),
157                message.cyan(), 
158                hash[..16].green().bold()
159        );
160    }
161    
162    // Show consistency
163    let test = "consistency";
164    let hash1 = sha256::hash(test)?;
165    let hash2 = sha256::hash(test)?;
166    println!("   πŸ” Consistency: {} β†’ {}", 
167            if hash1 == hash2 { "βœ… Always same result" } else { "❌ Error" },
168            if hash1 == hash2 { "Perfect!" } else { "Failed!" }
169    );
170    
171    println!("   ✨ Notice: Small input changes = Completely different hashes!");
172    println!("   πŸ’‘ Try: {}", "cargo run -- hash --sha256".bright_green());
173    
174    Ok(())
175}
176
177fn print_next_steps() {
178    println!("\n{}", "πŸŽ‰ Congratulations! You've mastered RusCrypt basics!".bright_green().bold());
179    println!();
180    
181    println!("{}", "πŸš€ Next Steps:".yellow().bold());
182    println!("   β€’ Run the full demo: {}", "cargo run --example demo".bright_cyan());
183    println!("   β€’ Try the CLI tool: {}", "cargo run -- --help".bright_cyan());
184    println!("   β€’ Explore algorithms: {}", "cargo run -- encrypt --help".bright_cyan());
185    println!();
186    
187    println!("{}", "πŸ“š Available Algorithms:".yellow().bold());
188    println!("   Classical:  Caesar, Vigenère, Playfair, Rail Fence");
189    println!("   Stream:     RC4 (educational)");
190    println!("   Block:      AES (secure), DES (educational)");
191    println!("   Asymmetric: RSA, Diffie-Hellman");
192    println!("   Hash:       MD5, SHA-1 (legacy), SHA-256 (secure)");
193    println!();
194    
195    println!("{}", "πŸ”’ Security Reminder:".red().bold());
196    println!("   βœ… Production: AES, RSA (β‰₯2048 bits), SHA-256");
197    println!("   πŸŽ“ Education: All classical ciphers, RC4, DES, MD5, SHA-1");
198    println!();
199    
200    println!("{}", "Built with ❀️  using Rust πŸ¦€".bright_blue().italic());
201}