mirror of
https://github.com/Steffo99/unimore-oop-2020-cleaver.git
synced 2024-11-21 23:54:20 +00:00
Start stitch job
This commit is contained in:
parent
7f274721ba
commit
66fe4f733d
9 changed files with 62 additions and 130 deletions
|
@ -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 <i>chopped</i> (*.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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package eu.steffo.cleaver.logic.stream.input;
|
||||
|
||||
public class CleaverDeflateInputStream implements ICleaverInputStream {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package eu.steffo.cleaver.logic.stream.input;
|
||||
|
||||
public class CleaverForkFileInputStream implements ICleaverInputStream {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package eu.steffo.cleaver.logic.stream.input;
|
||||
|
||||
public class CleaverSimpleFileInputStream implements ICleaverInputStream {
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
}
|
|
@ -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.");
|
Loading…
Reference in a new issue