Skip to content

Commit 194e246

Browse files
authored
Merge pull request #47 from Cryptographic-API-Services/#45-argon2-hash-multiple-passwords
#45 removing argon2 hashing multiple passwords
2 parents 2f7d4ad + 7d7e050 commit 194e246

File tree

1 file changed

+2
-122
lines changed

1 file changed

+2
-122
lines changed

src/argon2.rs

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
use std::{
2-
ffi::{c_char, CStr, CString}, slice, sync::mpsc, thread
2+
ffi::{c_char, CStr, CString}, sync::mpsc
33
};
4-
5-
extern crate rayon;
6-
7-
use std::sync::mpsc::{channel, Receiver};
8-
94
use argon2::{
105
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
116
Argon2,
127
};
13-
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
14-
15-
#[repr(C)]
16-
pub struct Argon2ThreadResult {
17-
pub passwords: *mut *mut c_char,
18-
pub length: usize,
19-
}
208

219
#[no_mangle]
2210
pub extern "C" fn argon2_verify(hashed_pass: *const c_char, password: *const c_char) -> bool {
@@ -116,50 +104,6 @@ fn argon2_verify_threadpool_test() {
116104
assert_eq!(true, is_valid);
117105
}
118106

119-
#[no_mangle]
120-
pub extern "C" fn argon2_verify_thread(
121-
hashed_pass: *const c_char,
122-
password: *const c_char,
123-
) -> bool {
124-
let hashed_pass_string = unsafe {
125-
assert!(!hashed_pass.is_null());
126-
CStr::from_ptr(hashed_pass)
127-
}
128-
.to_str()
129-
.unwrap();
130-
131-
let password_string = unsafe {
132-
assert!(!password.is_null());
133-
CStr::from_ptr(password)
134-
}
135-
.to_str()
136-
.unwrap()
137-
.as_bytes();
138-
let result = thread::spawn(move || {
139-
let parsed_hash = PasswordHash::new(&hashed_pass_string).unwrap();
140-
return Argon2::default()
141-
.verify_password(password_string, &parsed_hash)
142-
.is_ok();
143-
})
144-
.join()
145-
.unwrap();
146-
return result;
147-
}
148-
149-
#[test]
150-
fn argon2_verify_thread_test() {
151-
let password = "PasswordToVerify";
152-
let password_cstr = CString::new(password).unwrap();
153-
let password_bytes = password_cstr.as_bytes_with_nul();
154-
let password_ptr = password_bytes.as_ptr() as *const i8;
155-
let hashed_password = argon2_hash(password_ptr);
156-
let hashed_password_ctr = unsafe { CString::from_raw(hashed_password) };
157-
let hashed_password_bytes = hashed_password_ctr.as_bytes_with_nul();
158-
let hashed_password_ptr = hashed_password_bytes.as_ptr() as *const i8;
159-
let is_valid = argon2_verify_thread(hashed_password_ptr, password_ptr);
160-
assert_eq!(true, is_valid);
161-
}
162-
163107
#[no_mangle]
164108
pub extern "C" fn argon2_hash(pass_to_hash: *const c_char) -> *mut c_char {
165109
let pass_bytes = unsafe {
@@ -219,68 +163,4 @@ fn argon2_hash_threadpool_test() {
219163
let hashed_password_ctr = unsafe { CString::from_raw(hashed_password_ptr) };
220164
let hashed_password_str = hashed_password_ctr.to_str().unwrap();
221165
assert_ne!(password, hashed_password_str);
222-
}
223-
224-
#[no_mangle]
225-
pub extern "C" fn argon2_hash_thread(
226-
passwords_to_hash: *const *const c_char,
227-
num_of_passwords: usize,
228-
) -> Argon2ThreadResult {
229-
let mut parsed_passwords_to_hash: Vec<&[u8]> = Vec::new();
230-
unsafe {
231-
for i in 0..num_of_passwords {
232-
let c_str = CStr::from_ptr(*passwords_to_hash.offset(i as isize));
233-
let str_slice = c_str.to_str().unwrap().as_bytes();
234-
parsed_passwords_to_hash.push(str_slice);
235-
}
236-
}
237-
let (sender, receiver): (std::sync::mpsc::Sender<String>, Receiver<String>) = channel();
238-
let argon2 = Argon2::default();
239-
parsed_passwords_to_hash.par_iter_mut().for_each_with(
240-
sender.clone(),
241-
|task_sender, password| {
242-
let salt = SaltString::generate(&mut OsRng);
243-
let hashed_password = argon2.hash_password(password, &salt).unwrap().to_string();
244-
let _ = task_sender.send(hashed_password);
245-
},
246-
);
247-
let mut hashed_passwords: Vec<*mut i8> = Vec::new();
248-
for _ in 0..parsed_passwords_to_hash.len() {
249-
let result: String = receiver
250-
.recv()
251-
.expect("Error receiving hashed passsword result");
252-
hashed_passwords.push(CString::new(result).unwrap().into_raw());
253-
}
254-
let capacity = hashed_passwords.capacity();
255-
hashed_passwords.reserve_exact(capacity);
256-
let result = Argon2ThreadResult {
257-
passwords: hashed_passwords.as_mut_ptr(),
258-
length: hashed_passwords.len(),
259-
};
260-
std::mem::forget(hashed_passwords);
261-
return result;
262-
}
263-
264-
#[test]
265-
fn argon2_hash_thread_test() {
266-
let mut passwords_to_hash = Vec::new();
267-
passwords_to_hash.push(
268-
CString::new("welcome")
269-
.unwrap()
270-
.as_bytes_with_nul()
271-
.as_ptr() as *const i8,
272-
);
273-
passwords_to_hash.push(
274-
CString::new("welcome123")
275-
.unwrap()
276-
.as_bytes_with_nul()
277-
.as_ptr() as *const i8,
278-
);
279-
let passwords_length = passwords_to_hash.len();
280-
let result = argon2_hash_thread(passwords_to_hash.as_mut_ptr(), passwords_length);
281-
let result_slice = unsafe { slice::from_raw_parts(result.passwords, result.length) }.to_vec();
282-
assert_eq!(result_slice.len(), passwords_to_hash.len());
283-
for n in 0..result_slice.len() {
284-
assert_ne!(result_slice[n] as *const i8, passwords_to_hash[n])
285-
}
286-
}
166+
}

0 commit comments

Comments
 (0)