]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/hashing: method to SHA256 and finalize in one call
authorJason Ish <jason.ish@oisf.net>
Wed, 23 Dec 2020 22:24:23 +0000 (16:24 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 13 Jan 2021 08:01:04 +0000 (09:01 +0100)
Add SCSha256HashBuffer to hash a single buffer returning the
result.

rust/src/ffi/hashing.rs

index 3d0fef91e97255bc35e3d40d736ff0887bebe00f..b49aa1131c72e2c16c23e9b83b951055966ea50d 100644 (file)
@@ -76,6 +76,20 @@ pub unsafe extern "C" fn SCSha256Free(hasher: &mut SCSha256) {
     let _: Box<SCSha256> = Box::from_raw(hasher);
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn SCSha256HashBuffer(
+    buf: *const u8, buf_len: u32, out: *mut u8, len: u32,
+) -> bool {
+    if len as usize != SC_SHA256_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 = Sha256::new().chain(data).finalize();
+    output.copy_from_slice(&hash);
+    return true;
+}
+
 // Start of SHA1 C bindings.
 
 pub struct SCSha1(Sha1);