Skip to content

Commit

Permalink
feat: add gen_key() & gen_nonce()
Browse files Browse the repository at this point in the history
  • Loading branch information
AshGw committed May 31, 2024
1 parent 6ffe32d commit c64b017
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 36 deletions.
69 changes: 36 additions & 33 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ ctr = { version = "0.9.2", features = ["zeroize"] }
ghash = { version = "0.5.0", features = ["zeroize"] }
subtle = "2.3"
anyhow = "1.0.86"
pyo3 = "0.21.2"
pyo3 = { version = "0.18", features = ["extension-module", "abi3", "abi3-py37"] }
getrandom = "0.2"


3 changes: 3 additions & 0 deletions python/gcm_rs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from gcm_rs import *

print(sum_as_string(4,4))
Empty file removed python/gcm_rs/main.py
Empty file.
5 changes: 3 additions & 2 deletions rust/gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,12 @@ impl Decrypt for Aes256Gcm {
#[cfg(test)]
mod tests {
use super::*;
use crate::random::{gen_key, gen_nonce};

#[test]
fn test_aes256_gcm_encryption_decryption() {
let key = [0u8; 32];
let nonce = [0u8; 12];
let key = gen_key();
let nonce = gen_nonce();
let associated_data = b"associated_data";
let plaintext = b"plaintext";

Expand Down
15 changes: 15 additions & 0 deletions rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@ pub mod constants;
pub mod ctr;
pub mod error;
pub mod gcm;
pub mod random;
pub mod types;

use pyo3::prelude::*;

#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}

#[pymodule]
#[pyo3(name = "_lib_name")]
fn pyrust(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}
15 changes: 15 additions & 0 deletions rust/random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use getrandom::getrandom;

fn random_bytes(length: usize) -> Vec<u8> {
let mut bytes = vec![0u8; length];
getrandom(&mut bytes).expect("This should never happen");
bytes
}

pub fn gen_key() -> Vec<u8> {
random_bytes(32)
}

pub fn gen_nonce() -> Vec<u8> {
random_bytes(12)
}

0 comments on commit c64b017

Please sign in to comment.