Skip to content

Commit 8520c39

Browse files
authored
Merge pull request #56 from Cryptographic-API-Services/#1-zstd-compression
#55 zstd compression
2 parents d864967 + 54f72a2 commit 8520c39

File tree

4 files changed

+67
-20
lines changed

4 files changed

+67
-20
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cas_core_lib"
3-
version = "0.1.8"
3+
version = "0.1.9"
44
edition = "2021"
55
description = "This is a Rust library providing external facing functions to performant and trusted encryption in Rust"
66
license = "Apache-2.0"
@@ -14,4 +14,4 @@ crate-type = ["dylib"]
1414

1515
[dependencies]
1616
libc = "0.2.146"
17-
cas-lib = "0.1.6"
17+
cas-lib = "0.1.9"

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod rsa;
88
mod sha;
99
mod x25519;
1010
mod ascon_aead;
11+
mod zstd;
1112

1213
pub mod password_hashers {
1314
pub mod argon2;

src/password_hashers/argon2.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,6 @@ fn argon2_verify_test() {
3535
assert_eq!(true, is_valid);
3636
}
3737

38-
#[test]
39-
fn argon2_verify_fail_test() {
40-
let password = "PasswordToVerify";
41-
let password_cstr = CString::new(password).unwrap();
42-
let password_bytes = password_cstr.as_bytes_with_nul();
43-
let password_ptr = password_bytes.as_ptr() as *const i8;
44-
let hashed_password = argon2_hash(password_ptr);
45-
let hashed_password_ctr = unsafe { CString::from_raw(hashed_password) };
46-
let hashed_password_bytes = hashed_password_ctr.as_bytes_with_nul();
47-
let hashed_password_ptr = hashed_password_bytes.as_ptr() as *const i8;
48-
let bad_password = CString::new("NotTheFirstPassword")
49-
.unwrap()
50-
.as_bytes_with_nul()
51-
.as_ptr() as *const i8;
52-
let is_valid = argon2_verify(hashed_password_ptr, bad_password);
53-
assert_eq!(false, is_valid);
54-
}
55-
5638
#[no_mangle]
5739
pub extern "C" fn argon2_verify_threadpool(hashed_pass: *const c_char, password: *const c_char) -> bool {
5840
let hashed_pass_string = unsafe {

src/zstd/mod.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use cas_lib::compression::zstd;
2+
use libc::c_uchar;
3+
4+
#[repr(C)]
5+
pub struct ZstdCompressResult {
6+
data: *mut c_uchar,
7+
length: usize
8+
}
9+
10+
#[no_mangle]
11+
pub extern "C" fn decompress(data_to_decompress: *const c_uchar, data_to_decompress_length: usize) -> ZstdCompressResult {
12+
let data_to_decompress = unsafe { std::slice::from_raw_parts(data_to_decompress, data_to_decompress_length) }.to_vec();
13+
let mut decompressed_data = zstd::decompress(data_to_decompress);
14+
let capacity = decompressed_data.capacity();
15+
decompressed_data.reserve_exact(capacity);
16+
let result = ZstdCompressResult {
17+
data: decompressed_data.as_mut_ptr(),
18+
length: decompressed_data.len()
19+
};
20+
std::mem::forget(decompressed_data);
21+
result
22+
}
23+
24+
#[no_mangle]
25+
pub extern "C" fn decompress_threadpool(data_to_decompress: *const c_uchar, data_to_decompress_length: usize) -> ZstdCompressResult {
26+
let data_to_decompress = unsafe { std::slice::from_raw_parts(data_to_decompress, data_to_decompress_length) }.to_vec();
27+
let mut decompressed_data = zstd::decompress_threadpool(data_to_decompress);
28+
let capacity = decompressed_data.capacity();
29+
decompressed_data.reserve_exact(capacity);
30+
let result = ZstdCompressResult {
31+
data: decompressed_data.as_mut_ptr(),
32+
length: decompressed_data.len()
33+
};
34+
std::mem::forget(decompressed_data);
35+
result
36+
}
37+
38+
#[no_mangle]
39+
pub extern "C" fn compress(data_to_compress: *const c_uchar, data_to_compress_length: usize, level: usize) -> ZstdCompressResult {
40+
let data_to_compress = unsafe { std::slice::from_raw_parts(data_to_compress, data_to_compress_length) }.to_vec();
41+
let mut compressed_data = zstd::compress(data_to_compress, level as i32);
42+
let capacity = compressed_data.capacity();
43+
compressed_data.reserve_exact(capacity);
44+
let result = ZstdCompressResult {
45+
data: compressed_data.as_mut_ptr(),
46+
length: compressed_data.len()
47+
};
48+
std::mem::forget(compressed_data);
49+
result
50+
}
51+
52+
#[no_mangle]
53+
pub extern "C" fn compress_threadpool(data_to_compress: *const c_uchar, data_to_compress_length: usize, level: usize) -> ZstdCompressResult {
54+
let data_to_compress = unsafe { std::slice::from_raw_parts(data_to_compress, data_to_compress_length) }.to_vec();
55+
let mut compressed_data = zstd::compress_threadpool(data_to_compress, level as i32);
56+
let capacity = compressed_data.capacity();
57+
compressed_data.reserve_exact(capacity);
58+
let result = ZstdCompressResult {
59+
data: compressed_data.as_mut_ptr(),
60+
length: compressed_data.len()
61+
};
62+
std::mem::forget(compressed_data);
63+
result
64+
}

0 commit comments

Comments
 (0)