-
Notifications
You must be signed in to change notification settings - Fork 1
/
blake.rs
57 lines (49 loc) · 1.16 KB
/
blake.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use core::slice;
use safer_ffi::prelude::*;
#[derive_ReprC]
#[ReprC::opaque]
pub struct Hasher {
pub h: blake3::Hasher,
}
#[ffi_export]
pub fn hash_free(data: *mut u8) {
unsafe {
let p = std::slice::from_raw_parts_mut(data, 32).as_mut_ptr();
Box::from_raw(p);
}
}
#[ffi_export]
pub fn hash(data: *const u8, len: usize) -> *const u8 {
let v = unsafe { slice::from_raw_parts(data, len) };
let h = Box::new(*blake3::hash(&v).as_bytes());
Box::into_raw(h) as *const u8
}
impl Hasher {
pub fn new() -> Self {
Self {
h: ::blake3::Hasher::new(),
}
}
}
#[ffi_export]
pub fn hasher_new() -> repr_c::Box<Hasher> {
repr_c::Box::new(Hasher::new())
}
#[ffi_export]
pub fn hasher_update(hasher: &mut Hasher, data: *const u8, len: usize) {
let v = unsafe { slice::from_raw_parts(data, len) };
hasher.h.update(v);
}
#[ffi_export]
pub fn hasher_end(hasher: repr_c::Box<Hasher>) -> *const u8 {
let h = Box::new(*hasher.h.finalize().as_bytes());
drop(hasher);
Box::into_raw(h) as *const u8
}
#[::safer_ffi::cfg_headers]
#[test]
fn c_headers() -> ::std::io::Result<()> {
safer_ffi::headers::builder()
.to_file("h/blake3.h")?
.generate()
}