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