/// <summary> /// Zero the given buffer in a way that will not be optimized away. /// </summary> /// <typeparam name="T"> /// The type of the elements in the buffer. /// </typeparam> /// <param name="buffer"> /// The buffer to zero. /// </param> /// <param name="call"> /// The methods to call to secure the array. Defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>. /// </param> public static void Zero <T>(T[] buffer, SecureArrayCall call = null) where T : struct { var bufHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { IntPtr bufPtr = bufHandle.AddrOfPinnedObject(); UIntPtr cnt = new UIntPtr((uint)buffer.Length * (uint)BuiltInTypeElementSize(buffer)); (call?.ZeroMemory ?? DefaultCall.ZeroMemory)(bufPtr, cnt); } finally { bufHandle.Free(); } }
/// <summary> /// Initializes a new instance of the <see cref="SecureArray"/> class. /// </summary> /// <param name="call"> /// The methods that get called to secure the array. A null value defaults /// to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>. /// </param> /// <remarks> /// You cannot create a <see cref="SecureArray"/> directly, you must /// derive from this class like <see cref="SecureArray{T}"/> does. /// </remarks> protected SecureArray(SecureArrayCall call) { this.Call = call ?? DefaultCall; }
/// <summary> /// Initializes a new instance of the <see cref="SecureArray{T}"/> class. /// </summary> /// <param name="size"> /// The number of elements in the secure array. /// </param> /// <param name="call"> /// The methods that get called to secure the array. A null value /// defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>. /// </param> /// <remarks> /// Uses <see cref="SecureArrayType"/>.<see cref="SecureArrayType.ZeroedPinnedAndNoSwap"/>. /// </remarks> public SecureArray(int size, SecureArrayCall call) : base(call) { this.buf = new T[size]; this.Init(this.buf, SecureArrayType.ZeroedPinnedAndNoSwap); }
/// <summary> /// Initializes a new instance of the <see cref="SecureArray{T}"/> class. /// </summary> /// <param name="size"> /// The number of elements in the secure array. /// </param> /// <param name="type"> /// The type of secure array to initialize. /// </param> /// <param name="call"> /// The methods that get called to secure the array. A null value /// defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>. /// </param> public SecureArray(int size, SecureArrayType type, SecureArrayCall call) : base(call) { this.buf = new T[size]; this.Init(this.buf, type); }