Skip to content

Commit

Permalink
feat: rust bridge(not yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucisokiu committed Dec 30, 2024
1 parent ea2f6d3 commit 894e0ee
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 0 deletions.
307 changes: 307 additions & 0 deletions rust_bridge/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions rust_bridge/Cargo.toml
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"
9 changes: 9 additions & 0 deletions rust_bridge/src/api.rs
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()
}
35 changes: 35 additions & 0 deletions rust_bridge/src/encrypt/aes_256_gcm.rs
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())
}
}
23 changes: 23 additions & 0 deletions rust_bridge/src/encrypt/encrypt.rs
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)
}
}
3 changes: 3 additions & 0 deletions rust_bridge/src/encrypt/mod.rs
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;
Loading

0 comments on commit 894e0ee

Please sign in to comment.