Skip to content

#55 zstd compression #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cas_core_lib"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
description = "This is a Rust library providing external facing functions to performant and trusted encryption in Rust"
license = "Apache-2.0"
Expand All @@ -14,4 +14,4 @@ crate-type = ["dylib"]

[dependencies]
libc = "0.2.146"
cas-lib = "0.1.6"
cas-lib = "0.1.9"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod rsa;
mod sha;
mod x25519;
mod ascon_aead;
mod zstd;

pub mod password_hashers {
pub mod argon2;
Expand Down
18 changes: 0 additions & 18 deletions src/password_hashers/argon2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,6 @@ fn argon2_verify_test() {
assert_eq!(true, is_valid);
}

#[test]
fn argon2_verify_fail_test() {
let password = "PasswordToVerify";
let password_cstr = CString::new(password).unwrap();
let password_bytes = password_cstr.as_bytes_with_nul();
let password_ptr = password_bytes.as_ptr() as *const i8;
let hashed_password = argon2_hash(password_ptr);
let hashed_password_ctr = unsafe { CString::from_raw(hashed_password) };
let hashed_password_bytes = hashed_password_ctr.as_bytes_with_nul();
let hashed_password_ptr = hashed_password_bytes.as_ptr() as *const i8;
let bad_password = CString::new("NotTheFirstPassword")
.unwrap()
.as_bytes_with_nul()
.as_ptr() as *const i8;
let is_valid = argon2_verify(hashed_password_ptr, bad_password);
assert_eq!(false, is_valid);
}

#[no_mangle]
pub extern "C" fn argon2_verify_threadpool(hashed_pass: *const c_char, password: *const c_char) -> bool {
let hashed_pass_string = unsafe {
Expand Down
64 changes: 64 additions & 0 deletions src/zstd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use cas_lib::compression::zstd;
use libc::c_uchar;

#[repr(C)]
pub struct ZstdCompressResult {
data: *mut c_uchar,
length: usize
}

#[no_mangle]
pub extern "C" fn decompress(data_to_decompress: *const c_uchar, data_to_decompress_length: usize) -> ZstdCompressResult {
let data_to_decompress = unsafe { std::slice::from_raw_parts(data_to_decompress, data_to_decompress_length) }.to_vec();
let mut decompressed_data = zstd::decompress(data_to_decompress);
let capacity = decompressed_data.capacity();
decompressed_data.reserve_exact(capacity);
let result = ZstdCompressResult {
data: decompressed_data.as_mut_ptr(),
length: decompressed_data.len()
};
std::mem::forget(decompressed_data);
result
}

#[no_mangle]
pub extern "C" fn decompress_threadpool(data_to_decompress: *const c_uchar, data_to_decompress_length: usize) -> ZstdCompressResult {
let data_to_decompress = unsafe { std::slice::from_raw_parts(data_to_decompress, data_to_decompress_length) }.to_vec();
let mut decompressed_data = zstd::decompress_threadpool(data_to_decompress);
let capacity = decompressed_data.capacity();
decompressed_data.reserve_exact(capacity);
let result = ZstdCompressResult {
data: decompressed_data.as_mut_ptr(),
length: decompressed_data.len()
};
std::mem::forget(decompressed_data);
result
}

#[no_mangle]
pub extern "C" fn compress(data_to_compress: *const c_uchar, data_to_compress_length: usize, level: usize) -> ZstdCompressResult {
let data_to_compress = unsafe { std::slice::from_raw_parts(data_to_compress, data_to_compress_length) }.to_vec();
let mut compressed_data = zstd::compress(data_to_compress, level as i32);
let capacity = compressed_data.capacity();
compressed_data.reserve_exact(capacity);
let result = ZstdCompressResult {
data: compressed_data.as_mut_ptr(),
length: compressed_data.len()
};
std::mem::forget(compressed_data);
result
}

#[no_mangle]
pub extern "C" fn compress_threadpool(data_to_compress: *const c_uchar, data_to_compress_length: usize, level: usize) -> ZstdCompressResult {
let data_to_compress = unsafe { std::slice::from_raw_parts(data_to_compress, data_to_compress_length) }.to_vec();
let mut compressed_data = zstd::compress_threadpool(data_to_compress, level as i32);
let capacity = compressed_data.capacity();
compressed_data.reserve_exact(capacity);
let result = ZstdCompressResult {
data: compressed_data.as_mut_ptr(),
length: compressed_data.len()
};
std::mem::forget(compressed_data);
result
}
Loading