]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/hashing: function to md5 hash buffer to hex
authorJason Ish <jason.ish@oisf.net>
Wed, 30 Dec 2020 22:17:52 +0000 (16:17 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 13 Jan 2021 08:01:05 +0000 (09:01 +0100)
Add function SCMd5HashBufferToHex to hash a single buffer to an
md5 hex string.

rust/src/ffi/hashing.rs

index b49aa1131c72e2c16c23e9b83b951055966ea50d..c73606582e54a22c048b8a2de9e860b56ef239bf 100644 (file)
@@ -168,6 +168,25 @@ pub unsafe extern "C" fn SCMd5HashBuffer(buf: *const u8, buf_len: u32, out: *mut
     output.copy_from_slice(&hash);
 }
 
+/// C binding for a function to MD5 hash a single buffer to a hex string.
+#[no_mangle]
+pub unsafe extern "C" fn SCMd5HashBufferToHex(
+    buf: *const u8, buf_len: u32, out: *mut c_char, len: u32,
+) {
+    let out = &mut *(out as *mut u8);
+    let output = std::slice::from_raw_parts_mut(out, len as usize);
+    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 = Md5::new().chain(data).finalize();
+    let hex = format!("{:x}", &hash);
+
+    // This will panic if the sizes differ.
+    output[0..len as usize - 1].copy_from_slice(&hex.as_bytes());
+
+    // Terminate the string.
+    output[output.len() - 1] = 0;
+}
+
 // Functions that are generic over Digest. For the most part the C bindings are
 // just wrappers around these.