rust/hashing: new function to SHA1 hash a single buffer

SCSha1HashBuffer will has a single buffer and compute the digest
in one call.
pull/5722/head
Jason Ish 4 years ago committed by Victor Julien
parent ff37526c6b
commit eb5cfd9b82

@ -15,12 +15,13 @@
* 02110-1301, USA.
*/
use std::os::raw::c_char;
use digest::Digest;
use md5::Md5;
use sha1::Sha1;
use sha2::Sha256;
use std::os::raw::c_char;
pub const SC_SHA1_LEN: usize = 20;
pub const SC_SHA256_LEN: usize = 32;
// Wrap the Rust Sha256 in a new type named SCSha256 to give this type
@ -103,6 +104,20 @@ pub unsafe extern "C" fn SCSha1Free(hasher: &mut SCSha1) {
let _: Box<SCSha1> = Box::from_raw(hasher);
}
#[no_mangle]
pub unsafe extern "C" fn SCSha1HashBuffer(
buf: *const u8, buf_len: u32, out: *mut u8, len: u32,
) -> bool {
if len as usize != SC_SHA1_LEN {
return false;
}
let data = std::slice::from_raw_parts(buf, buf_len as usize);
let output = std::slice::from_raw_parts_mut(out, len as usize);
let hash = Sha1::new().chain(data).finalize();
output.copy_from_slice(&hash);
return true;
}
// Start of MD5 C bindins.
pub struct SCMd5(Md5);

Loading…
Cancel
Save