diff --git a/src/eu/steffo/cleaver/logic/job/ChopJob.java b/src/eu/steffo/cleaver/logic/job/ChopJob.java index a391e2e..8510dd3 100644 --- a/src/eu/steffo/cleaver/logic/job/ChopJob.java +++ b/src/eu/steffo/cleaver/logic/job/ChopJob.java @@ -18,8 +18,6 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.*; -import java.nio.file.Path; -import java.nio.file.Paths; /** * A {@link Job} that converts regular files into chopped (*.chp + *.cXX) files. @@ -103,7 +101,7 @@ public class ChopJob extends Job { } if(compressConfig instanceof DeflateConfig) { - outputStream = new CleaverDeflaterOutputStream(outputStream); + outputStream = new CleaverDeflateOutputStream(outputStream); } if(cryptConfig instanceof PasswordConfig) { diff --git a/src/eu/steffo/cleaver/logic/job/StitchJob.java b/src/eu/steffo/cleaver/logic/job/StitchJob.java index a8dce3d..9054414 100644 --- a/src/eu/steffo/cleaver/logic/job/StitchJob.java +++ b/src/eu/steffo/cleaver/logic/job/StitchJob.java @@ -3,7 +3,7 @@ package eu.steffo.cleaver.logic.job; import eu.steffo.cleaver.errors.ChpFileError; import eu.steffo.cleaver.errors.ProgrammingError; import eu.steffo.cleaver.logic.config.*; -import eu.steffo.cleaver.logic.stream.input.CryptInputStream; +import eu.steffo.cleaver.logic.stream.input.*; import eu.steffo.cleaver.logic.progress.ErrorProgress; import eu.steffo.cleaver.logic.progress.FinishedProgress; import eu.steffo.cleaver.logic.progress.Progress; @@ -123,13 +123,36 @@ public class StitchJob extends Job { } /** - * Read a {@link Document} and set the {@link IConfig}, {@link PasswordConfig} and {@link DeflateConfig} of this job accordingly. - * @param doc The {@link Document} to be read. - * @param cryptKey The encryption key to use in the {@link PasswordConfig}. - * @throws ChpFileError If there's an error while parsing the *.chp file. + * Read a {@link Element} and create a {@link ICleaverInputStream} based on it. + * @param element The {@link Element} to be read. + * @param cryptKey The encryption key to use in case a {@link CleaverCryptInputStream} is created. + * @throws ChpFileError If there's an error while parsing the node. */ - protected void parseChp(Document doc, String cryptKey) throws ChpFileError { - //TODO + protected ICleaverInputStream parseNode(Element element, String cryptKey) throws ChpFileError { + String name = element.getTagName(); + + ICleaverInputStream result; + + if(name.equals("Crypt")) { + //TODO + } + else if(name.equals("Deflate")) { + //TODO + } + else if(name.equals("Fork")) { + //TODO + } + else if(name.equals("Simple")) { + //TODO + } + else if(name.equals("Split")) { + //TODO + } + else { + throw new ChpFileError("Unknown tag: " + name); + } + + return result; } @Override diff --git a/src/eu/steffo/cleaver/logic/stream/input/CleaverCryptInputStream.java b/src/eu/steffo/cleaver/logic/stream/input/CleaverCryptInputStream.java new file mode 100644 index 0000000..d5b3878 --- /dev/null +++ b/src/eu/steffo/cleaver/logic/stream/input/CleaverCryptInputStream.java @@ -0,0 +1,11 @@ +package eu.steffo.cleaver.logic.stream.input; + +import java.io.FilterInputStream; +import java.io.InputStream; + +public class CleaverCryptInputStream extends FilterInputStream implements ICleaverInputStream { + + protected CleaverCryptInputStream(InputStream in) { + super(in); + } +} diff --git a/src/eu/steffo/cleaver/logic/stream/input/CleaverDeflateInputStream.java b/src/eu/steffo/cleaver/logic/stream/input/CleaverDeflateInputStream.java new file mode 100644 index 0000000..c599ef7 --- /dev/null +++ b/src/eu/steffo/cleaver/logic/stream/input/CleaverDeflateInputStream.java @@ -0,0 +1,4 @@ +package eu.steffo.cleaver.logic.stream.input; + +public class CleaverDeflateInputStream implements ICleaverInputStream { +} diff --git a/src/eu/steffo/cleaver/logic/stream/input/CleaverForkFileInputStream.java b/src/eu/steffo/cleaver/logic/stream/input/CleaverForkFileInputStream.java new file mode 100644 index 0000000..ee18970 --- /dev/null +++ b/src/eu/steffo/cleaver/logic/stream/input/CleaverForkFileInputStream.java @@ -0,0 +1,4 @@ +package eu.steffo.cleaver.logic.stream.input; + +public class CleaverForkFileInputStream implements ICleaverInputStream { +} diff --git a/src/eu/steffo/cleaver/logic/stream/input/CleaverSimpleFileInputStream.java b/src/eu/steffo/cleaver/logic/stream/input/CleaverSimpleFileInputStream.java new file mode 100644 index 0000000..cc49a5a --- /dev/null +++ b/src/eu/steffo/cleaver/logic/stream/input/CleaverSimpleFileInputStream.java @@ -0,0 +1,4 @@ +package eu.steffo.cleaver.logic.stream.input; + +public class CleaverSimpleFileInputStream implements ICleaverInputStream { +} diff --git a/src/eu/steffo/cleaver/logic/stream/input/CleaverSplitFileInputStream.java b/src/eu/steffo/cleaver/logic/stream/input/CleaverSplitFileInputStream.java index e069bfd..9fd8d3b 100644 --- a/src/eu/steffo/cleaver/logic/stream/input/CleaverSplitFileInputStream.java +++ b/src/eu/steffo/cleaver/logic/stream/input/CleaverSplitFileInputStream.java @@ -4,7 +4,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -public class CleaverSplitFileInputStream extends InputStream { +public class CleaverSplitFileInputStream extends InputStream implements ICleaverInputStream { private final String fileBaseName; private long currentByteCount; private long maximumByteCount; diff --git a/src/eu/steffo/cleaver/logic/stream/input/CryptInputStream.java b/src/eu/steffo/cleaver/logic/stream/input/CryptInputStream.java deleted file mode 100644 index 8185322..0000000 --- a/src/eu/steffo/cleaver/logic/stream/input/CryptInputStream.java +++ /dev/null @@ -1,112 +0,0 @@ -package eu.steffo.cleaver.logic.stream.input; - -import javax.crypto.Cipher; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.SecretKeySpec; -import java.io.FilterInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.KeySpec; - -public class CryptInputStream extends FilterInputStream { - private Cipher cipher; - - /** - * @return The algorithm used for the encryption. - */ - public String getAlgorithm() { - return "AES"; - } - - /** - * @return The mode of operation used for the encryption. - */ - public String getModeOfOperation() { - return "CFB8"; - } - - /** - * @return The padding used for the encryption. - */ - public String getPadding() { - return "PKCS5Padding"; - } - - /** - * @return The secret key algorithm used in the generation of the final key. - */ - public String getKeyAlgorithm() { - return "PBKDF2WithHmacSHA1"; - } - - /** - * @return The full transformation string as required by {@link Cipher#getInstance(String)}. - */ - public String getTransformationString() { - return String.format("%s/%s/%s", getAlgorithm(), getModeOfOperation(), getPadding()); - } - - - public CryptInputStream(InputStream in, String key) throws InvalidKeyException { - super(in); - - //Setup the cipher object - try { - cipher = Cipher.getInstance(getTransformationString()); - } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { - //Should never happen, as it's predefined - e.printStackTrace(); - return; - } - - //Create the salt - byte[] salt = new byte[8]; - SecureRandom secureRandom = new SecureRandom(); - secureRandom.nextBytes(salt); - - //Create the KeySpec - //Using the recommended 65536 as iteration count - KeySpec spec = new PBEKeySpec(key.toCharArray(), salt, 65536, 256); - - SecretKeyFactory factory; - try { - factory = SecretKeyFactory.getInstance(getKeyAlgorithm()); - } catch (NoSuchAlgorithmException e) { - //Should never happen, as it's predefined - e.printStackTrace(); - return; - } - - //Create the pbkdf secret key - SecretKey pbkdf; - try { - pbkdf = factory.generateSecret(spec); - } catch (InvalidKeySpecException e) { - //Should never happen, as it's predefined - e.printStackTrace(); - return; - } - - //"Convert" the secret key to a AES secret key - SecretKey aes = new SecretKeySpec(pbkdf.getEncoded(), getAlgorithm()); - - //Init the cipher instance - cipher.init(Cipher.DECRYPT_MODE, aes); - } - - @Override - public int read() throws IOException { - int encryptedInt = super.read(); - byte[] encryptedByte = new byte[1]; - encryptedByte[0] = (byte)encryptedInt; - byte[] decryptedByte = cipher.update(encryptedByte); - return decryptedByte[0]; - } -} diff --git a/src/eu/steffo/cleaver/logic/stream/output/CleaverDeflaterOutputStream.java b/src/eu/steffo/cleaver/logic/stream/output/CleaverDeflateOutputStream.java similarity index 85% rename from src/eu/steffo/cleaver/logic/stream/output/CleaverDeflaterOutputStream.java rename to src/eu/steffo/cleaver/logic/stream/output/CleaverDeflateOutputStream.java index 2325845..3835b13 100644 --- a/src/eu/steffo/cleaver/logic/stream/output/CleaverDeflaterOutputStream.java +++ b/src/eu/steffo/cleaver/logic/stream/output/CleaverDeflateOutputStream.java @@ -8,12 +8,12 @@ import java.io.OutputStream; import java.util.zip.Deflater; import java.util.zip.DeflaterOutputStream; -public class CleaverDeflaterOutputStream extends DeflaterOutputStream implements ICleaverOutputStream { +public class CleaverDeflateOutputStream extends DeflaterOutputStream implements ICleaverOutputStream { /** * Construct a new CleaverCompressOutputStream and ensure the passed {@link OutputStream} implements {@link ICleaverOutputStream}. * @see DeflaterOutputStream#DeflaterOutputStream(OutputStream, Deflater, int, boolean) */ - public CleaverDeflaterOutputStream(OutputStream out, Deflater def, int size, boolean syncFlush) { + public CleaverDeflateOutputStream(OutputStream out, Deflater def, int size, boolean syncFlush) { super(out, def, size, syncFlush); if(!(out instanceof ICleaverOutputStream)) { throw new IllegalArgumentException("The OutputStream passed to the CleaverCompressOutputStream must implement ICleaverOutputStream."); @@ -24,7 +24,7 @@ public class CleaverDeflaterOutputStream extends DeflaterOutputStream implements * Construct a new CleaverCompressOutputStream and ensure the passed {@link OutputStream} implements {@link ICleaverOutputStream}. * @see DeflaterOutputStream#DeflaterOutputStream(OutputStream, Deflater, int) */ - public CleaverDeflaterOutputStream(OutputStream out, Deflater def, int size) { + public CleaverDeflateOutputStream(OutputStream out, Deflater def, int size) { super(out, def, size); if(!(out instanceof ICleaverOutputStream)) { throw new IllegalArgumentException("The OutputStream passed to the CleaverCompressOutputStream must implement ICleaverOutputStream."); @@ -35,7 +35,7 @@ public class CleaverDeflaterOutputStream extends DeflaterOutputStream implements * Construct a new CleaverCompressOutputStream and ensure the passed {@link OutputStream} implements {@link ICleaverOutputStream}. * @see DeflaterOutputStream#DeflaterOutputStream(OutputStream, Deflater, boolean) */ - public CleaverDeflaterOutputStream(OutputStream out, Deflater def, boolean syncFlush) { + public CleaverDeflateOutputStream(OutputStream out, Deflater def, boolean syncFlush) { super(out, def, syncFlush); if(!(out instanceof ICleaverOutputStream)) { throw new IllegalArgumentException("The OutputStream passed to the CleaverCompressOutputStream must implement ICleaverOutputStream."); @@ -46,7 +46,7 @@ public class CleaverDeflaterOutputStream extends DeflaterOutputStream implements * Construct a new CleaverCompressOutputStream and ensure the passed {@link OutputStream} implements {@link ICleaverOutputStream}. * @see DeflaterOutputStream#DeflaterOutputStream(OutputStream, Deflater) */ - public CleaverDeflaterOutputStream(OutputStream out, Deflater def) { + public CleaverDeflateOutputStream(OutputStream out, Deflater def) { super(out, def); if(!(out instanceof ICleaverOutputStream)) { throw new IllegalArgumentException("The OutputStream passed to the CleaverCompressOutputStream must implement ICleaverOutputStream."); @@ -57,7 +57,7 @@ public class CleaverDeflaterOutputStream extends DeflaterOutputStream implements * Construct a new CleaverCompressOutputStream and ensure the passed {@link OutputStream} implements {@link ICleaverOutputStream}. * @see DeflaterOutputStream#DeflaterOutputStream(OutputStream, boolean) */ - public CleaverDeflaterOutputStream(OutputStream out, boolean syncFlush) { + public CleaverDeflateOutputStream(OutputStream out, boolean syncFlush) { super(out, syncFlush); if(!(out instanceof ICleaverOutputStream)) { throw new IllegalArgumentException("The OutputStream passed to the CleaverCompressOutputStream must implement ICleaverOutputStream."); @@ -68,7 +68,7 @@ public class CleaverDeflaterOutputStream extends DeflaterOutputStream implements * Construct a new CleaverCompressOutputStream and ensure the passed {@link OutputStream} implements {@link ICleaverOutputStream}. * @see DeflaterOutputStream#DeflaterOutputStream(OutputStream) */ - public CleaverDeflaterOutputStream(OutputStream out) { + public CleaverDeflateOutputStream(OutputStream out) { super(out); if(!(out instanceof ICleaverOutputStream)) { throw new IllegalArgumentException("The OutputStream passed to the CleaverCompressOutputStream must implement ICleaverOutputStream.");