diff --git a/src/org/blueshard/cryptogx/SecureDelete.java b/src/org/blueshard/cryptogx/SecureDelete.java index 6b3a49e..4c5dcb3 100644 --- a/src/org/blueshard/cryptogx/SecureDelete.java +++ b/src/org/blueshard/cryptogx/SecureDelete.java @@ -1,154 +1,54 @@ package org.blueshard.cryptogx; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Random; +import java.util.TreeSet; +/** + *

Class for secure delete files

+ * + * @since 1.2.0 + */ public class SecureDelete { - /** - *

Overwrites the file {@param iterations} times at once with random bytes an delete it

- * - * @see SecureDelete#deleteFileAllInOne(File, int) - */ - public static boolean deleteFileAllInOne(String filename, int iterations) throws IOException, NoSuchAlgorithmException { - return deleteFileAllInOne(new File(filename), iterations); - } - - /** - *

Overwrites the file {@param iterations} times at once with random bytes and delete it

- * - * @param file that should be deleted - * @param iterations how many times the file should be overwritten before it gets deleted - * @return if the file could be deleted - * @throws IOException - * @throws NoSuchAlgorithmException - */ - public static boolean deleteFileAllInOne(File file, int iterations) throws IOException, NoSuchAlgorithmException { - long fileLength = file.length() + 1 ; - for (int i=0; i 1000000000) { - int numOfByteArrays = (int) Math.ceil((double) fileLength / 1000000000); - for (int len=0; len directories = new TreeSet<>(); + Files.walk(Paths.get(directory)).map(Path::toFile).forEach(directoryFile -> { + if (directoryFile.isDirectory()) { + directories.add(directoryFile); } else { - byte[] randomBytes = new byte[new Random().nextInt((int) fileLength)]; - SecureRandom.getInstanceStrong().nextBytes(randomBytes); - bufferedOutputStream.write(randomBytes); - } - bufferedOutputStream.flush(); - bufferedOutputStream.close(); - } - - return file.delete(); - } - - /** - *

Overwrites the file {@param iterations} times at once with random bytes (minimal size {@param minFileSize}; maximal size {@param maxFileSize}) and delete it

- * - * @see SecureDelete#deleteFileAllInOne(String, int, long, long) - */ - public static boolean deleteFileAllInOne(String filename, int iterations, long minFileSize, long maxFileSize) throws IOException, NoSuchAlgorithmException { - return deleteFileAllInOne(new File(filename), iterations, minFileSize, maxFileSize); - } - - /** - *

Overwrites the file {@param iterations} times at once with random bytes (minimal size {@param minFileSize}; maximal size {@param maxFileSize}) and delete it

- * - * @param file that should be deleted - * @param iterations how many times the file should be overwritten before it gets deleted - * @param minFileSize is the minimal file size for every {@param iterations} - * @param maxFileSize is the maximal file size for every {@param iterations} - * @return if the file could be deleted - * @throws IOException - * @throws NoSuchAlgorithmException - */ - public static boolean deleteFileAllInOne(File file, int iterations, long minFileSize, long maxFileSize) throws IOException, NoSuchAlgorithmException { - for (int i = 0; i < iterations; i++) { - BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); - if (maxFileSize > 1000000000) { - int numOfByteArrays = (int) Math.ceil((double) maxFileSize / 1000000000); - for (int len = 0; len < numOfByteArrays; len++) { - int newMaxFileSize = (int) maxFileSize / numOfByteArrays; - int newMinFileSize = 0; - if (minFileSize != 0) { - newMinFileSize = (int) minFileSize / numOfByteArrays; - } - byte[] randomBytes = new byte[new Random().nextInt(newMaxFileSize - newMinFileSize) + newMinFileSize]; - SecureRandom.getInstanceStrong().nextBytes(randomBytes); - bufferedOutputStream.write(randomBytes); + try { + SecureDelete.deleteFile(directoryFile, iterations, buffer); + } catch (IOException e) { + e.printStackTrace(); } - } else { - byte[] randomBytes = new byte[new Random().nextInt((int) maxFileSize - (int) minFileSize) + (int) minFileSize]; - SecureRandom.getInstanceStrong().nextBytes(randomBytes); - bufferedOutputStream.write(randomBytes); - } - bufferedOutputStream.flush(); - bufferedOutputStream.close(); - } - - return file.delete(); - } - - /** - *

Overwrites the file {@param iterations} times line by line with random bytes and delete it

- * - * @see SecureDelete#deleteFileLineByLine(File, int) - */ - public static boolean deleteFileLineByLine(String filename, int iterations) throws NoSuchAlgorithmException, IOException { - return deleteFileLineByLine(new File(filename), iterations); - } - - /** - *

Overwrites the file {@param iterations} times line by line with random bytes and delete it

- * - * @param file that should be deleted - * @param iterations how many times the file should be overwritten before it gets deleted - * @return if the file could be deleted - * @throws IOException - * @throws NoSuchAlgorithmException - */ - public static boolean deleteFileLineByLine(File file, int iterations) throws NoSuchAlgorithmException, IOException { - long fileLength = file.length() + 1 ; - for (int i=0; i 1000000000) { - int numOfByteArrays = (int) Math.ceil((double) fileLength / 1000000000); - for (int len=0; lenOverwrites the file {@param iterations} times line by line with random bytes (minimal size {@param minFileSize}; maximal size {@param maxFileSize}) and delete it

- */ - public static boolean deleteFileLineByLine(String filename, int iterations, long minFileSize, long maxFileSize) throws NoSuchAlgorithmException, IOException { - return deleteFileLineByLine(new File(filename), iterations, minFileSize, maxFileSize); } /** @@ -156,41 +56,35 @@ public class SecureDelete { * * @param file that should be deleted * @param iterations how many times the file should be overwritten before it gets deleted - * @param minFileSize is the minimal file size for every {@param iterations} - * @param maxFileSize is the maximal file size for every {@param iterations} * @return if the file could be deleted * @throws IOException - * @throws NoSuchAlgorithmException + * + * @since 1.12.0 */ - public static boolean deleteFileLineByLine(File file, int iterations, long minFileSize, long maxFileSize) throws NoSuchAlgorithmException, IOException { - for (int i=0; i 1000000000) { - int numOfByteArrays = (int) Math.ceil((double) maxFileSize / 1000000000); - for (int len=0; len