1 // Written in the D programming language.
2 
3 module wrapper.sodium.crypto_hash_sha256;
4 
5 import wrapper.sodium.core; // assure sodium got initialized
6 
7 public
8 import  deimos.sodium.crypto_hash_sha256 : crypto_hash_sha256_state,
9                                            crypto_hash_sha256_statebytes,
10                                            crypto_hash_sha256_BYTES,
11                                            crypto_hash_sha256_bytes;
12 /*                                         crypto_hash_sha256,
13                                            crypto_hash_sha256_init,
14                                            crypto_hash_sha256_update,
15                                            crypto_hash_sha256_final; */
16 
17 alias crypto_hash_sha256 = deimos.sodium.crypto_hash_sha256.crypto_hash_sha256;
18 
19 pragma(inline, true)
20 bool crypto_hash_sha256(out ubyte[crypto_hash_sha256_BYTES] out_, scope const ubyte[] in_) @nogc pure @trusted
21 {
22   return  crypto_hash_sha256(out_.ptr, in_.ptr, in_.length) == 0;
23 }
24 
25 
26 alias crypto_hash_sha256_init = deimos.sodium.crypto_hash_sha256.crypto_hash_sha256_init;
27 
28 pragma(inline, true)
29 bool crypto_hash_sha256_init(out crypto_hash_sha256_state state) @nogc pure @trusted
30 {
31   return  crypto_hash_sha256_init(&state) == 0;
32 }
33 
34 alias crypto_hash_sha256_update = deimos.sodium.crypto_hash_sha256.crypto_hash_sha256_update;
35 
36 pragma(inline, true)
37 bool crypto_hash_sha256_update(ref crypto_hash_sha256_state state, scope const ubyte[] in_) @nogc pure @trusted
38 {
39   return  crypto_hash_sha256_update(&state, in_.ptr, in_.length) == 0;
40 }
41 
42 alias crypto_hash_sha256_final = deimos.sodium.crypto_hash_sha256.crypto_hash_sha256_final;
43 
44 pragma(inline, true)
45 bool crypto_hash_sha256_final(ref crypto_hash_sha256_state state, out ubyte[crypto_hash_sha256_BYTES] out_) @nogc pure @trusted
46 {
47   return  crypto_hash_sha256_final(&state, out_.ptr) == 0;
48 }
49 
50 
51 @safe
52 unittest
53 {
54   import std.stdio : writeln, writefln;
55   import std..string : representation;
56   debug writeln("unittest block 1 from sodium.crypto_hash_sha256.d");
57 
58 //  assert(crypto_hash_sha256_statebytes == crypto_hash_sha256_state.sizeof);
59 //  writeln(crypto_hash_sha256_statebytes()); // 104
60 //  writeln(crypto_hash_sha256_state.sizeof); // 112
61   assert(crypto_hash_sha256_bytes() == crypto_hash_sha256_BYTES);
62 
63   auto message = representation("test some more text!");
64 
65   ubyte[crypto_hash_sha256_BYTES]  hash;
66   ubyte[crypto_hash_sha256_BYTES]  hash_saved;
67   assert(crypto_hash_sha256(hash, message));
68   hash_saved = hash;
69 //  writefln("0x%(%02X%)", hash);  // 0xC01F1C6165CB6E3E28690B05D7DDCF8FEF28E8719E2F719800681058C08C9970
70   hash = hash.init;
71   auto message1 = representation("test some");
72   auto message2 = representation(" more text!");
73   crypto_hash_sha256_state  state;
74 
75   assert(crypto_hash_sha256_init  (state));
76   assert(crypto_hash_sha256_update(state, message1));
77   assert(crypto_hash_sha256_update(state, message2));
78   assert(crypto_hash_sha256_final (state, hash));
79   assert(hash == hash_saved);
80 }