1 module blake3.binding; 2 3 extern (C) pure nothrow @nogc: 4 5 // Copied from BLAKE3/c/blake3.h 6 7 enum BLAKE3_VERSION_STRING = "1.8.5"; 8 enum BLAKE3_KEY_LEN = 32, 9 BLAKE3_OUT_LEN = 32, 10 BLAKE3_BLOCK_LEN = 64, 11 BLAKE3_CHUNK_LEN = 1024, 12 BLAKE3_MAX_DEPTH = 54; 13 14 // This struct is a private implementation detail. It has to be here because 15 // it's part of the blake3_hasher structure defined below. 16 struct blake3_chunk_state { 17 uint[8] cv; 18 ulong chunk_counter; 19 ubyte[BLAKE3_BLOCK_LEN] buf; 20 ubyte buf_len; 21 ubyte blocks_compressed; 22 ubyte flags; 23 } 24 25 struct blake3_hasher { 26 uint[8] key; 27 blake3_chunk_state chunk; 28 ubyte cv_stack_len; 29 // The stack size is MAX_DEPTH + 1 because we do lazy merging. For example, 30 // with 7 chunks, we have 3 entries in the stack. Adding an 8th chunk 31 // requires a 4th entry, rather than merging everything down to 1, because we 32 // don't know whether more input is coming. This is different from how the 33 // reference implementation does things. 34 ubyte[(BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN] cv_stack; 35 } 36 37 const(char)* blake3_version() @safe; 38 void blake3_hasher_init(blake3_hasher* self); 39 void blake3_hasher_init_keyed( 40 blake3_hasher* self, 41 ref const(ubyte)[BLAKE3_KEY_LEN] key); 42 void blake3_hasher_init_derive_key(blake3_hasher* self, const(char)* context); 43 void blake3_hasher_init_derive_key_raw( 44 blake3_hasher* self, 45 const(void)* context, 46 size_t context_len); 47 void blake3_hasher_update( 48 blake3_hasher* self, 49 const(void)* input, 50 size_t input_len); 51 version (BLAKE3_USE_TBB) { 52 void blake3_hasher_update_tbb( 53 blake3_hasher* self, 54 const(void)* input, 55 size_t input_len); 56 } 57 void blake3_hasher_finalize( 58 const(blake3_hasher)* self, 59 ubyte* out_, 60 size_t out_len); 61 void blake3_hasher_finalize_seek( 62 const(blake3_hasher)* self, 63 ulong seek, 64 ubyte* out_, 65 size_t out_len); 66 void blake3_hasher_reset(blake3_hasher* self);