How to wipe a file completely in C#
By Tan Lee Published on Nov 05, 2024 343
To ensure a file is completely erased and unrecoverable, various secure wiping standards are used, including (British HMG IS5, Russian GOST, US Army AR380-19, German VSITR, etc)
HMG IS5 is a data destruction standard used by the British government. The HMG IS5 Baseline method securely wipes data by overwriting a file with 0s. The more advanced HMG IS5 Enhanced method involves three passes: first overwriting with 0s, then with 1s, and finally with randomly generated bytes to ensure complete data destruction.
Here is a sample code for implementing the HMG IS5 Baseline method, which securely wipes a file by overwriting it with 0s
class HMGIS5 { public static void WipeFile(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); } }
By using this approach, you are adhering to secure file wiping practices, which is in line with HMG IS5 guidelines.
- Primitive types in C#
- How to set permissions for a directory in C#
- How to Convert Int to Byte Array in C#
- How to Convert string list to int list in C#
- How to convert timestamp to date in C#
- How to Get all files in a folder in C#
- How to use Channel as an async queue in C#
- Case sensitivity in JSON deserialization