Update SecureDelete.java

This commit is contained in:
blueShard-dev 2020-06-26 09:16:58 +00:00 committed by GitHub
parent 4b5b951b7b
commit 92a7dea2e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
/**
* <p>Class for secure delete files<p/>
*
* @since 1.2.0
*/
public class SecureDelete {
/**
* <p>Overwrites the file {@param iterations} times at once with random bytes an delete it</p>
*
* @see SecureDelete#deleteFileAllInOne(File, int)
*/
public static boolean deleteFileAllInOne(String filename, int iterations) throws IOException, NoSuchAlgorithmException {
return deleteFileAllInOne(new File(filename), iterations);
}
/**
* <p>Overwrites the file {@param iterations} times at once with random bytes and delete it</p>
*
* @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<iterations; i++) {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
if (fileLength > 1000000000) {
int numOfByteArrays = (int) Math.ceil((double) fileLength / 1000000000);
for (int len=0; len<numOfByteArrays; len++) {
int newMaxFileSize = (int) fileLength / numOfByteArrays;
int newMinFileSize = 0;
byte[] randomBytes = new byte[new Random().nextInt(newMaxFileSize - newMinFileSize) + newMinFileSize];
SecureRandom.getInstanceStrong().nextBytes(randomBytes);
bufferedOutputStream.write(randomBytes);
}
public static void deleteDirectory(String directory, int iterations, byte[] buffer) throws IOException {
TreeSet<File> 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);
try {
SecureDelete.deleteFile(directoryFile, iterations, buffer);
} catch (IOException e) {
e.printStackTrace();
}
while (directoryFile.exists()) {
if (directoryFile.delete()) {
break;
}
}
}
});
File deleteDirectory = directories.last();
while (deleteDirectory != null) {
deleteDirectory.delete();
while (deleteDirectory.delete()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
return file.delete();
deleteDirectory = directories.lower(deleteDirectory);
}
/**
* <p>Overwrites the file {@param iterations} times at once with random bytes (minimal size {@param minFileSize}; maximal size {@param maxFileSize}) and delete it</p>
*
* @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);
}
/**
* <p>Overwrites the file {@param iterations} times at once with random bytes (minimal size {@param minFileSize}; maximal size {@param maxFileSize}) and delete it</p>
*
* @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);
}
} 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();
}
/**
* <p>Overwrites the file {@param iterations} times line by line with random bytes and delete it</p>
*
* @see SecureDelete#deleteFileLineByLine(File, int)
*/
public static boolean deleteFileLineByLine(String filename, int iterations) throws NoSuchAlgorithmException, IOException {
return deleteFileLineByLine(new File(filename), iterations);
}
/**
* <p>Overwrites the file {@param iterations} times line by line with random bytes and delete it</p>
*
* @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<iterations; i++) {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
if (fileLength > 1000000000) {
int numOfByteArrays = (int) Math.ceil((double) fileLength / 1000000000);
for (int len=0; len<numOfByteArrays; len++) {
int newMaxFileSize = (int) fileLength / numOfByteArrays;
int newMinFileSize = 0;
byte[] randomBytes = new byte[new Random().nextInt(newMaxFileSize - newMinFileSize) + newMinFileSize];
SecureRandom.getInstanceStrong().nextBytes(randomBytes);
for (byte b: randomBytes) {
bufferedOutputStream.write(b);
}
}
} else {
byte[] randomBytes = new byte[new Random().nextInt((int) fileLength)];
SecureRandom.getInstanceStrong().nextBytes(randomBytes);
for (byte b : randomBytes) {
bufferedOutputStream.write(b);
}
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
return file.delete();
}
/**
* <p>Overwrites the file {@param iterations} times line by line with random bytes (minimal size {@param minFileSize}; maximal size {@param maxFileSize}) and delete it</p>
*/
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 {
public static void deleteFile(File file, int iterations, byte[] buffer) throws IOException {
SecureRandom secureRandom = new SecureRandom();
RandomAccessFile raf = new RandomAccessFile(file, "rws");
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);
for (byte b: randomBytes) {
bufferedOutputStream.write(b);
}
}
} else {
byte[] randomBytes = new byte[new Random().nextInt((int) maxFileSize - (int) minFileSize) + (int) minFileSize];
SecureRandom.getInstanceStrong().nextBytes(randomBytes);
for (byte b : randomBytes) {
bufferedOutputStream.write(b);
}
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
return file.delete();
long length = file.length();
raf.seek(0);
raf.getFilePointer();
int pos = 0;
while (pos < length) {
secureRandom.nextBytes(buffer);
raf.write(buffer);
pos += buffer.length;
}
}
raf.close();
while (file.delete()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}