1 module dcrypt.util.util; 2 3 import core.vararg; 4 import std.traits; 5 import std.algorithm; 6 7 @safe @nogc nothrow 8 void wipe(T)(ref T t) { 9 static if(isArray!T) { 10 t[] = 0; 11 assert(all!"a == 0"(t[]), "Failed to wipe ubyte[]."); 12 } else static if ( is(typeof( {T a = T.init;} ))) { 13 t = T.init; 14 } else { 15 static assert(false, "Type not supported for wiping: " ~ T.stringof); 16 } 17 } 18 19 20 @safe @nogc nothrow 21 void wipe(T...)(ref T ts) { 22 foreach(ref t; ts) { 23 wipe(t); 24 } 25 } 26 27 // test static arrays 28 unittest { 29 ubyte[4] buf1 = [1,2,3,4]; 30 uint[4] buf2 = [1,2,3,4]; 31 size_t[4] buf3 = [1,2,3,4]; 32 33 wipe(buf1); 34 wipe(buf2); 35 wipe(buf3); 36 37 assert(all!"a == 0"(buf1[]), "Failed to wipe ubyte[]."); 38 assert(all!"a == 0"(buf2[]), "Failed to wipe ubyte[]."); 39 assert(all!"a == 0"(buf3[]), "Failed to wipe ubyte[]."); 40 } 41 42 // test dynamic arrays 43 unittest { 44 ubyte[] buf1 = [1,2,3,4]; 45 uint[] buf2 = [1,2,3,4]; 46 size_t[] buf3 = [1,2,3,4]; 47 48 wipe(buf1, buf2, buf3); 49 50 assert(all!"a == 0"(buf1), "Failed to wipe ubyte[]."); 51 assert(all!"a == 0"(buf2), "Failed to wipe ubyte[]."); 52 assert(all!"a == 0"(buf3), "Failed to wipe ubyte[]."); 53 } 54 55 unittest { 56 int a = 42; 57 int b = 84; 58 ubyte c = 1; 59 60 wipe(a, b, c); 61 62 assert(a == 0 && b == 0 && c == 0, "Wiping integer failed!"); 63 }