C# Tips

C# Tip Article

How to wipe a file completely

We might want to wipe a file completely so that the file can never be recovered. Some of wiping standards are British HMG IS5, Russian GOST, US Army AR380-19, German VSITR, etc.

Let's look at one of them, HMG IS5. HMG Infosec Standard 5 (IS5 in short) is a data destruction standard used by the British government. HMG IS5 Baseline is a wipe method that overwrites 0s to all data in the file. And more enhanced one, HMG IS5 Enhanced, is using three passes; first overwrites with 0s, then with 1s, and then with randomly generated bytes.

Here is a sample code for HMG IS5 Baseline.

class HMG_IS5
{
	public static void WipeFileBaseline(string filePath)
	{
		using (var fs = File.OpenWrite(filePath))
		{
			// Make 0s
			var bytes = Enumerable.Repeat<byte>(0x00, (int)fs.Length).ToArray();

			// Write 0s to file
			fs.Write(bytes, 0, bytes.Length);
		}

		// And delete the file
		File.Delete(filePath);
	}
}