From eb5cfd9b822c39d2555dbfe539c9d609617dee5e Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Wed, 23 Dec 2020 09:47:32 -0600 Subject: [PATCH] rust/hashing: new function to SHA1 hash a single buffer SCSha1HashBuffer will has a single buffer and compute the digest in one call. --- rust/src/ffi/hashing.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/rust/src/ffi/hashing.rs b/rust/src/ffi/hashing.rs index 66f0b804fe..3d0fef91e9 100644 --- a/rust/src/ffi/hashing.rs +++ b/rust/src/ffi/hashing.rs @@ -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 = 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);