1 module chloride.core; 2 3 import std.array : uninitializedArray; 4 import std..string : fromStringz; 5 import std.exception : ErrnoException; 6 7 import deimos.sodium.core; 8 9 static this() { 10 if (sodium_init() == -1) { 11 throw new SodiumException("Unable to initialize Sodium"); 12 } 13 } 14 15 /** 16 * Exception class for errors that happen in libsodium. 17 */ 18 class SodiumException : ErrnoException { 19 this(string msg, string file = __FILE__, size_t line = __LINE__) @safe { 20 super(msg, file, line); 21 } 22 } 23 24 /** 25 * enforce a condition, and if it fails throw a `SodiumException` with a message retrieved 26 * from the errno. 27 */ 28 void enforceSodium(bool condition, string file = __FILE__, size_t line = __LINE__) @safe { 29 if (!condition) { 30 throw new SodiumException("Sodium error", file, line); 31 } 32 }