use crate::ffi::{Buffer, Slice};
pub use crate::helpers::sha1::Sha1;
#[unsafe(no_mangle)]
pub extern "C" fn soyokaze_sha1_block_size() -> usize {
crate::helpers::sha1::BLOCK_SIZE
}
#[unsafe(no_mangle)]
pub extern "C" fn soyokaze_sha1_digest_size() -> usize {
crate::helpers::sha1::DIGEST_SIZE
}
#[unsafe(no_mangle)]
pub extern "C" fn soyokaze_sha1_initial_state() -> *const u32 {
crate::helpers::sha1::INITIAL_STATE.as_ptr()
}
#[unsafe(no_mangle)]
pub extern "C" fn soyokaze_sha1_constants() -> *const u32 {
crate::helpers::sha1::CONSTANTS.as_ptr()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_sha1(data: *const u8, data_len: usize) -> Buffer {
let data = unsafe { Slice::borrow(data, data_len) }.unwrap_or_default();
Buffer::new(crate::helpers::sha1::sha1(data).to_vec())
}
#[unsafe(no_mangle)]
pub extern "C" fn soyokaze_sha1_new() -> *mut Sha1 {
Box::into_raw(Box::new(Sha1::new()))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_sha1_free(hash: *mut Sha1) {
if !hash.is_null() {
drop(unsafe { Box::from_raw(hash) });
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_sha1_update(hash: *mut Sha1, data: *const u8, data_len: usize) -> bool {
let (Some(hash), Some(data)) = (unsafe { hash.as_mut() }, unsafe { Slice::borrow(data, data_len) }) else {
return false;
};
hash.update(data);
true
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_sha1_compress(hash: *mut Sha1, block: *const u8, block_len: usize) -> bool {
let (Some(hash), Some(block)) = (unsafe { hash.as_mut() }, unsafe { Slice::borrow(block, block_len) }) else {
return false;
};
let Ok(block) = <&[u8; crate::helpers::sha1::BLOCK_SIZE]>::try_from(block) else {
return false;
};
hash.compress(block);
true
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_sha1_finish(hash: *mut Sha1, out: *mut Buffer) -> bool {
if hash.is_null() {
return false;
}
let digest = unsafe { Box::from_raw(hash) }.finish();
if !out.is_null() {
unsafe { *out = Buffer::new(digest.to_vec()) };
}
true
}