-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ceaa2d
commit a867f04
Showing
7 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ members = [ | |
"problem-solving", | ||
# projects | ||
"projects/pandas", | ||
"projects/ruscrypt", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "ruscrypt" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Ruscrypt | ||
|
||
`ruscrypt` is a basic encryption and decryption project that can encrypt and | ||
decrypt messages with passwords. | ||
|
||
This library demonstrates the basics of encrypting the data which is not safe | ||
in production use, however you can use it as a fun project and encrypt and | ||
decrypt your basic messages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
mod ruscrypt; | ||
|
||
use ruscrypt::Crypto; | ||
|
||
fn main() { | ||
let crypto = Crypto::new("test".to_owned()); | ||
let encrypted = crypto.encrypt("This is a original text".to_owned()); | ||
println!("Encrypted: {encrypted}"); | ||
let decrypted = crypto.decrypt(encrypted); | ||
println!("Decrypted: {decrypted}"); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::ruscrypt::Crypto; | ||
|
||
#[test] | ||
fn test_encrypt_decrypt() { | ||
let key = String::from("My Encryption Key"); | ||
let message = String::from("This is my Secret Message"); | ||
|
||
let crypt = Crypto::new(key); | ||
assert_eq!(crypt.decrypt(crypt.encrypt(message.clone())), message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
pub(crate) struct Crypto { | ||
key: Vec<u8>, | ||
} | ||
|
||
impl Crypto { | ||
pub(crate) fn new(key: String) -> Self { | ||
Self { | ||
key: key.as_bytes().to_owned(), | ||
} | ||
} | ||
|
||
pub(crate) fn encrypt(&self, message: String) -> String { | ||
let len = self.key.len(); | ||
let enc = message | ||
.bytes() | ||
.enumerate() | ||
.map(|(idx, ch)| { | ||
let target = self.key[idx % len]; | ||
(target ^ ch) as char | ||
// format!("{:0x}", target ^ ch) | ||
}) | ||
.collect::<String>(); | ||
return enc; | ||
} | ||
|
||
pub(crate) fn decrypt(&self, message: String) -> String { | ||
let len = self.key.len(); | ||
let dec = message | ||
.bytes() | ||
.enumerate() | ||
.map(|(idx, ch)| { | ||
let target = self.key[idx % len]; | ||
(target ^ ch) as char | ||
}) | ||
.collect::<String>(); | ||
return dec; | ||
} | ||
} |