-
Notifications
You must be signed in to change notification settings - Fork 6
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
Showing
7 changed files
with
393 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "rust_brigde" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
aes-gcm = "0.10.3" | ||
hex = "0.4.3" | ||
rand = "0.8.5" | ||
crypto = "0.5.1" |
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,9 @@ | ||
use crate::encrypt::EncryptAES; | ||
|
||
pub fn encrypt_aes256(cleartext: &str, key: Option<&str>) -> String { | ||
EncryptAES::encrypt_aes256(cleartext, key).unwrap() | ||
} | ||
|
||
pub fn decrypt_aes256(ciphertext: &str, key: Option<&str>) -> String { | ||
EncryptAES::decrypt_aes256(ciphertext, key).unwrap() | ||
} |
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,35 @@ | ||
use aes_gcm::{Aes256Gcm as Aes256GcmLib, Nonce, aead::{Aead, KeyInit}}; | ||
use hex; | ||
use rand::Rng; | ||
|
||
const KEY_LENGTH: usize = 32; | ||
const IV_LENGTH: usize = 12; | ||
|
||
pub struct Aes256Gcm; | ||
|
||
impl Aes256Gcm { | ||
pub fn encrypt(cleartext: &str, key: &str) -> Result<String, Box<dyn std::error::Error>> { | ||
let key_bytes = key.as_bytes(); | ||
let cleartext_bytes = cleartext.as_bytes(); | ||
|
||
let cipher = Aes256GcmLib::new_from_slice(key_bytes).unwrap(); | ||
let mut nonce = [0u8; IV_LENGTH]; | ||
rand::thread_rng().fill(&mut nonce); // Randomize nonce using rand crate | ||
|
||
let ciphertext = cipher.encrypt(&Nonce::from_slice(&nonce), cleartext_bytes).unwrap(); | ||
let result = [nonce.as_ref(), ciphertext.as_slice()].concat(); | ||
|
||
Ok(hex::encode(result)) | ||
} | ||
|
||
pub fn decrypt(ciphertext: &str, key: &str) -> Result<String, Box<dyn std::error::Error>> { | ||
let key_bytes = key.as_bytes(); | ||
let cipher_bytes = hex::decode(ciphertext).unwrap(); | ||
let (nonce, ciphertext) = cipher_bytes.split_at(IV_LENGTH); | ||
|
||
let cipher = Aes256GcmLib::new_from_slice(key_bytes).unwrap(); | ||
let decrypted_data = cipher.decrypt(&Nonce::from_slice(nonce), ciphertext).unwrap(); | ||
|
||
Ok(String::from_utf8(decrypted_data).unwrap()) | ||
} | ||
} |
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,23 @@ | ||
use crate::encrypt::aes_256_gcm::Aes256Gcm; | ||
|
||
pub struct EncryptAES; | ||
|
||
impl EncryptAES { | ||
pub fn encrypt_aes256(cleartext: &str, key: Option<&str>) -> Result<String, Box<dyn std::error::Error>> { | ||
let keyword = key.unwrap_or("waterbus"); | ||
if keyword.is_empty() { | ||
return Ok(cleartext.to_string()); | ||
} | ||
|
||
Aes256Gcm::encrypt(cleartext, keyword) | ||
} | ||
|
||
pub fn decrypt_aes256(ciphertext: &str, key: Option<&str>) -> Result<String, Box<dyn std::error::Error>> { | ||
let keyword = key.unwrap_or("waterbus"); | ||
if keyword.is_empty() { | ||
return Ok(ciphertext.to_string()); | ||
} | ||
|
||
Aes256Gcm::decrypt(ciphertext, keyword) | ||
} | ||
} |
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,3 @@ | ||
pub mod aes_256_gcm; | ||
pub mod encrypt; | ||
pub use encrypt::EncryptAES; |
Oops, something went wrong.