rust/hashing: function to md5 hash buffer to hex

Add function SCMd5HashBufferToHex to hash a single buffer to an
md5 hex string.
pull/5722/head
Jason Ish 6 years ago committed by Victor Julien
parent e00d21a5cb
commit 26c1321668

@ -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.

Loading…
Cancel
Save