com.github.isaichkindanila.crypt.lib.file.FileDecryptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of crypt-lib Show documentation
Show all versions of crypt-lib Show documentation
Simple Java library for stream ciphers based encryption
The newest version!
package com.github.isaichkindanila.crypt.lib.file;
import com.github.isaichkindanila.crypt.lib.stream.DecryptedInputStream;
import com.github.isaichkindanila.crypt.lib.stream.IncorrectPasswordException;
import com.github.isaichkindanila.crypt.lib.stream.MalformedStreamHeaderException;
import com.github.isaichkindanila.crypt.lib.stream.UnsupportedCipherException;
import com.github.isaichkindanila.crypt.lib.util.IOUtils;
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
* Class used to decrypt files encrypted by {@code FileEncryptor}.
*
* @see FileEncryptor
*/
@SuppressWarnings("WeakerAccess")
public class FileDecryptor {
private final IOHandler ioHandler;
private final InputStream in;
private final File targetDir;
private final long total;
/**
* Equivalent to {@link #FileDecryptor(InputStream, File, String) FileDecryptor(new FileInputStream(encryptedFile), targetDir, password)}.
*
* @param encryptedFile encrypted file
* @param targetDir directory where decrypted files will be put
* @param password password to be used to decrypt input stream
* @throws IOException if IOException occurs while reading stream header
* @throws MalformedStreamHeaderException if stream header is malformed
* @throws UnsupportedCipherException if the stream was encrypted with cipher which is not supported by this version of Crypt Lib
* @throws IncorrectPasswordException if the stream was encrypted with different password
*/
public FileDecryptor(File encryptedFile, File targetDir, String password) throws IOException {
this(new FileInputStream(encryptedFile), targetDir, password);
}
/**
* Accepts {@code InputStream} to allow decrypting resources accessed via URL.
*
* @param encryptedStream encrypted input stream
* @param targetDir directory where decrypted files will be put
* @param password password to be used to decrypt input stream
* @throws IOException if IOException occurs while reading stream header
* @throws MalformedStreamHeaderException if stream header is malformed
* @throws UnsupportedCipherException if the stream was encrypted with cipher which is not supported by this version of Crypt Lib
* @throws IncorrectPasswordException if the stream was encrypted with different password
*/
public FileDecryptor(InputStream encryptedStream, File targetDir, String password) throws IOException {
if (!targetDir.exists() && !targetDir.mkdirs()) {
throw new FileNotFoundException("directory '" + targetDir.getAbsolutePath() + "' does not exist and cannot be created");
}
this.in = new DecryptedInputStream(encryptedStream, password);
this.targetDir = targetDir;
ioHandler = new IOHandler();
total = IOUtils.readLong(in);
}
private void decryptFile() throws IOException {
byte[] pathBytes = IOUtils.readByteArray(in);
long size = IOUtils.readLong(in);
String path = new String(pathBytes, StandardCharsets.UTF_8);
File file = new File(targetDir, path);
ioHandler.createFile(in, file, size);
}
/**
* Decrypts file passed in constructor.
*
* @throws IOException if IOException occurs
*/
public void decrypt() throws IOException {
int fileCount = IOUtils.readInt(in);
for (int i = 0; i < fileCount; i++) {
decryptFile();
}
in.close();
}
/**
* Returns value in range [0, 1] representing decryption progress,
* where 0 means "not started yet" and 1 means "already finished".
*
* @return double value in range [0, 1]
*/
public double progress() {
return total == 0 ? 1.0 : 1.0 * ioHandler.done / total;
}
}