1 // Written in the D programming language.
2 
3 module wrapper.sodium.crypto_shorthash;
4 
5 import wrapper.sodium.core; // assure sodium got initialized
6 
7 public import  deimos.sodium.crypto_shorthash: crypto_shorthash_BYTES,
8                                                crypto_shorthash_bytes,
9                                                crypto_shorthash_KEYBYTES,
10                                                crypto_shorthash_keybytes,
11                                                crypto_shorthash_PRIMITIVE,
12                                                crypto_shorthash_primitive,
13 //                                             crypto_shorthash,
14                                                crypto_shorthash_keygen;
15 import deimos.sodium.crypto_shorthash_siphash24 : crypto_shorthash_siphash24_BYTES,
16                                                   crypto_shorthash_siphash24_KEYBYTES;
17 
18 alias crypto_shorthash = deimos.sodium.crypto_shorthash.crypto_shorthash;
19 
20 pragma(inline, true)
21 int crypto_shorthash(out ubyte[crypto_shorthash_BYTES] hash, scope const ubyte[] data, const ubyte[crypto_shorthash_KEYBYTES] key) @nogc pure @trusted
22 {
23   return crypto_shorthash(hash.ptr, data.ptr, data.length, key.ptr); // __attribute__ ((nonnull(1, 4)));
24 }
25 
26 
27 /*pure*/ @system
28 unittest {
29   import std.stdio : writeln, writefln;
30   import std.string : fromStringz, representation;
31   import std.algorithm.comparison : equal;
32   import wrapper.sodium.randombytes : randombytes;
33 
34   debug writeln("unittest block 1 from sodium.crypto_shorthash.d");
35   assert(crypto_shorthash_bytes()    == crypto_shorthash_BYTES);    //  8U
36   assert(crypto_shorthash_keybytes() == crypto_shorthash_KEYBYTES); // 16U
37   assert(equal(fromStringz(crypto_shorthash_primitive()), crypto_shorthash_PRIMITIVE));
38 
39   auto short_data = representation("Sparkling water");
40 
41   ubyte[crypto_shorthash_BYTES]    hash;
42   ubyte[crypto_shorthash_KEYBYTES] key;
43   randombytes(key);
44   crypto_shorthash(hash.ptr, short_data.ptr, short_data.length, key.ptr);
45 //  writefln("shorthash: 0x%(%02x%)", hash);
46 //  writefln("key:       0x%(%02x%)", key);
47   ubyte[crypto_shorthash_BYTES]    hash_saved = hash;
48 
49 // overload
50   hash = hash.init;
51   crypto_shorthash(hash, short_data, key);
52 //  writefln("shorthash: 0x%(%02x%)", hash);
53   assert(hash == hash_saved);
54 //shorthash: 0xeeec84f5bce8afe9
55 //shorthash: 0xeeec84f5bce8afe9
56 //key:       0x86fb325ac11ef888257e9415b60db7a1
57   ubyte[crypto_shorthash_KEYBYTES] k;
58   crypto_shorthash_keygen(k);
59 }