1 module wrapper.sodium.crypto_verify_16;
2 
3 import wrapper.sodium.core; // assure sodium got initialized
4 
5 public
6 import  deimos.sodium.crypto_verify_16 : crypto_verify_16_BYTES,
7                                          crypto_verify_16_bytes;
8 //                                       crypto_verify_16
9 
10 
11 // overloading a functions between module deimos.sodium.crypto_verify_16 and this module
12 
13 alias crypto_verify_16 = deimos.sodium.crypto_verify_16.crypto_verify_16;
14 
15 /** Secrets are always compared in constant time using sodium_memcmp() or crypto_verify_(16|32|64)()
16  * @returns true, if the content of array `x` matches the content of array `y`.
17  * Otherwise, it returns false.
18  */
19 pragma(inline, true)
20 bool  crypto_verify_16(const ubyte[crypto_verify_16_BYTES] x, const ubyte[crypto_verify_16_BYTES] y) @nogc nothrow pure @trusted
21 {
22   return  crypto_verify_16(x.ptr, y.ptr) == 0; // __attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
23 }
24 
25 
26 pure @system
27 unittest
28 {
29   import std.stdio : writeln;
30   import std.range : iota, array;
31   debug  writeln("unittest block 1 from sodium.crypto_verify_16.d");
32 
33 //crypto_verify_16
34   ubyte[] buf1 = array(iota(ubyte(1), cast(ubyte)(1+crypto_verify_16_BYTES))); // [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
35   ubyte[] buf2 = buf1.dup;
36   assert(crypto_verify_16(buf1.ptr, buf2.ptr) ==  0);
37   buf2[$-1] = 17;
38   assert(crypto_verify_16(buf1.ptr, buf2.ptr) == -1);
39 }
40 
41 @nogc nothrow pure @safe
42 unittest
43 {
44   import std.range : iota, enumerate;
45 //crypto_verify_16_bytes
46   assert(crypto_verify_16_bytes() == crypto_verify_16_BYTES);
47 
48 //crypto_verify_16 overload
49   ubyte[crypto_verify_16_BYTES] buf1 = void; // = array(iota(ubyte(1), cast(ubyte)(1+crypto_verify_16_BYTES)))[];
50 version(GNU) // 	GDC (GNU D Compiler) is the compiler ; there, enumerate is not @nogc
51 {
52   size_t idx;
53   foreach (e; iota(ubyte(1), cast(ubyte)(1+crypto_verify_16_BYTES)))
54     buf1[idx++] = e;
55 }
56 else {
57   foreach (i, e; iota(ubyte(1), cast(ubyte)(1+crypto_verify_16_BYTES)).enumerate)
58     buf1[i] = e;
59 }
60   ubyte[crypto_verify_16_BYTES] buf2 = buf1;
61   assert( crypto_verify_16(buf1, buf2));
62   buf2[$-1] = 17;
63   assert(!crypto_verify_16(buf1, buf2));
64 }