Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Dec 22, 2024
1 parent 125ed2f commit f8f6b6c
Show file tree
Hide file tree
Showing 161 changed files with 833 additions and 1,107 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ru/olegcherednik/zip4jvm/crypto/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package ru.olegcherednik.zip4jvm.crypto;

import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.io.in.DataInput;

import java.io.IOException;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ru/olegcherednik/zip4jvm/crypto/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package ru.olegcherednik.zip4jvm.crypto;

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.DataOutput;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package ru.olegcherednik.zip4jvm.crypto;

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.DataOutput;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ru.olegcherednik.zip4jvm.crypto.Decoder;
import ru.olegcherednik.zip4jvm.crypto.strong.DecryptionHeader;
import ru.olegcherednik.zip4jvm.io.ByteOrder;
import ru.olegcherednik.zip4jvm.model.EncryptionMethod;

import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -45,32 +46,33 @@ public static AesCentralDirectoryDecoder create128(DecryptionHeader decryptionHe
char[] password,
long compressedSize,
ByteOrder byteOrder) throws IOException {
return create(decryptionHeader, password, AesStrength.S128, compressedSize, byteOrder);
return create(decryptionHeader, password, EncryptionMethod.AES_STRONG_128, compressedSize, byteOrder);
}

@SuppressWarnings("NewMethodNamingConvention")
public static AesCentralDirectoryDecoder create192(DecryptionHeader decryptionHeader,
char[] password,
long compressedSize,
ByteOrder byteOrder) throws IOException {
return create(decryptionHeader, password, AesStrength.S192, compressedSize, byteOrder);
return create(decryptionHeader, password, EncryptionMethod.AES_STRONG_192, compressedSize, byteOrder);
}

@SuppressWarnings("NewMethodNamingConvention")
public static AesCentralDirectoryDecoder create256(DecryptionHeader decryptionHeader,
char[] password,
long compressedSize,
ByteOrder byteOrder) throws IOException {
return create(decryptionHeader, password, AesStrength.S256, compressedSize, byteOrder);
return create(decryptionHeader, password, EncryptionMethod.AES_STRONG_256, compressedSize, byteOrder);
}

private static AesCentralDirectoryDecoder create(DecryptionHeader decryptionHeader,
char[] password,
AesStrength strength,
EncryptionMethod encryptionMethod,
long compressedSize,
ByteOrder byteOrder) throws IOException {
AesStrength strength = AesStrength.of(encryptionMethod);
Cipher cipher = AesStrongEngine.createCipher(decryptionHeader, password, strength, byteOrder);
AesStrongEngine engine = new AesStrongEngine(cipher);
AesStrongEngine engine = new AesStrongEngine(encryptionMethod, cipher);
return new AesCentralDirectoryDecoder(engine, compressedSize);
}

Expand All @@ -88,4 +90,11 @@ public int decrypt(byte[] buf, int offs, int len) {
return engine.decrypt(buf, offs, len);
}

// ---------- Object ----------

@Override
public String toString() {
return engine.getEncryptionMethod().getTitle();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import ru.olegcherednik.zip4jvm.crypto.Decoder;
import ru.olegcherednik.zip4jvm.exception.IncorrectZipEntryPasswordException;
import ru.olegcherednik.zip4jvm.exception.Zip4jvmException;
import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.io.in.DataInput;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

import lombok.AccessLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package ru.olegcherednik.zip4jvm.crypto.aes;

import ru.olegcherednik.zip4jvm.crypto.Encoder;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.DataOutput;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package ru.olegcherednik.zip4jvm.crypto.aes;

import ru.olegcherednik.zip4jvm.model.EncryptionMethod;

import lombok.Getter;

import java.security.SecureRandom;
Expand Down Expand Up @@ -69,12 +71,28 @@ public byte[] generateSalt() {
return buf;
}

public static AesStrength parseValue(int code) {
public static AesStrength of(int code) {
for (AesStrength aesKeyStrength : values())
if (aesKeyStrength.getCode() == code)
return aesKeyStrength;

throw new EnumConstantNotPresentException(AesStrength.class, "code=" + code);
}

public static AesStrength of(EncryptionMethod encryptionMethod) {
switch (encryptionMethod) {
case AES_128:
case AES_STRONG_128:
return S128;
case AES_192:
case AES_STRONG_192:
return S192;
case AES_256:
case AES_STRONG_256:
return S256;
default:
return NULL;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import ru.olegcherednik.zip4jvm.crypto.strong.EncryptionAlgorithm;
import ru.olegcherednik.zip4jvm.exception.IncorrectPasswordException;
import ru.olegcherednik.zip4jvm.exception.IncorrectZipEntryPasswordException;
import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.io.in.DataInput;
import ru.olegcherednik.zip4jvm.io.readers.crypto.strong.DecryptionHeaderReader;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;
Expand All @@ -43,6 +43,7 @@ public final class AesStrongDecoder implements Decoder {

private static final String DECRYPTION_HEADER = "AesStrongDecoder.DecryptionHeader";

private final EncryptionAlgorithm encryptionAlgorithm;
private final Cipher cipher;
@Getter
private final long compressedSize;
Expand All @@ -51,17 +52,17 @@ public final class AesStrongDecoder implements Decoder {

public static AesStrongDecoder create(ZipEntry zipEntry, DataInput in) throws IOException {
in.mark(DECRYPTION_HEADER);
Cipher cipher = createCipher(zipEntry, in);
DecryptionHeader decryptionHeader = new DecryptionHeaderReader().read(in);
Cipher cipher = createCipher(decryptionHeader, zipEntry, in);
int decryptionHeaderSize = (int) in.getMarkSize(DECRYPTION_HEADER);
long compressedSize = zipEntry.getCompressedSize() - decryptionHeaderSize;
return new AesStrongDecoder(cipher, compressedSize);
return new AesStrongDecoder(decryptionHeader.getEncryptionAlgorithm(), cipher, compressedSize);
}

private static Cipher createCipher(ZipEntry zipEntry, DataInput in) throws IOException {
DecryptionHeader decryptionHeader = new DecryptionHeaderReader().read(in);
EncryptionAlgorithm encryptionAlgorithm = decryptionHeader.getEncryptionAlgorithm();

private static Cipher createCipher(DecryptionHeader decryptionHeader, ZipEntry zipEntry, DataInput in)
throws IOException {
try {
EncryptionAlgorithm encryptionAlgorithm = decryptionHeader.getEncryptionAlgorithm();
return encryptionAlgorithm.createCipher(decryptionHeader, zipEntry.getPassword(), in.getByteOrder());
} catch (IncorrectPasswordException e) {
throw new IncorrectZipEntryPasswordException(zipEntry.getFileName());
Expand Down Expand Up @@ -103,4 +104,11 @@ private static int unpad(byte[] buf, int offs, int len) {
return len - n;
}

// ---------- Object ----------

@Override
public String toString() {
return encryptionAlgorithm.getTitle();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import ru.olegcherednik.zip4jvm.crypto.strong.DecryptionHeader;
import ru.olegcherednik.zip4jvm.exception.IncorrectPasswordException;
import ru.olegcherednik.zip4jvm.io.ByteOrder;
import ru.olegcherednik.zip4jvm.model.EncryptionMethod;
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.NotImplementedException;
Expand All @@ -43,6 +45,8 @@
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public final class AesStrongEngine implements Engine {

@Getter
private final EncryptionMethod encryptionMethod;
private final Cipher cipher;

public int getBlockSize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package ru.olegcherednik.zip4jvm.crypto.pkware;

import ru.olegcherednik.zip4jvm.crypto.Decoder;
import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.io.in.DataInput;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package ru.olegcherednik.zip4jvm.crypto.pkware;

import ru.olegcherednik.zip4jvm.crypto.Encoder;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.DataOutput;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package ru.olegcherednik.zip4jvm.crypto.pkware;

import ru.olegcherednik.zip4jvm.exception.IncorrectZipEntryPasswordException;
import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.in.DataInput;
import ru.olegcherednik.zip4jvm.io.out.DataOutput;
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry;

import lombok.AccessLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
package ru.olegcherednik.zip4jvm.crypto.strong;

import ru.olegcherednik.zip4jvm.io.ByteOrder;
import ru.olegcherednik.zip4jvm.io.in.buf.ByteArrayDataInput;
import ru.olegcherednik.zip4jvm.io.in.data.DataInput;

import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -88,12 +86,7 @@ public static long getActualCrc32(byte[] passwordValidationData) {

@SuppressWarnings("NewMethodNamingConvention")
public static long getExpectedCrc32(byte[] passwordValidationData, ByteOrder byteOrder) throws IOException {
final int len = 4;
final int offs = passwordValidationData.length - len;

try (DataInput in = new ByteArrayDataInput(passwordValidationData, offs, len, byteOrder)) {
return in.readDword();
}
return byteOrder.readDword(passwordValidationData, passwordValidationData.length - 4);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import ru.olegcherednik.zip4jvm.ZipFile;
import ru.olegcherednik.zip4jvm.exception.Zip4jvmException;
import ru.olegcherednik.zip4jvm.io.in.data.RandomAccessDataInput;
import ru.olegcherednik.zip4jvm.io.in.RandomAccessDataInput;
import ru.olegcherednik.zip4jvm.io.in.file.SolidLittleEndianDataInputFile;
import ru.olegcherednik.zip4jvm.io.in.file.SplitLittleEndianDataInputFile;
import ru.olegcherednik.zip4jvm.model.AesVersion;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ru/olegcherednik/zip4jvm/engine/ZipEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import ru.olegcherednik.zip4jvm.engine.np.NamedPath;
import ru.olegcherednik.zip4jvm.exception.EntryDuplicationException;
import ru.olegcherednik.zip4jvm.exception.EntryNotFoundException;
import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.SolidZipDataOutput;
import ru.olegcherednik.zip4jvm.io.out.data.SplitZipDataOutput;
import ru.olegcherednik.zip4jvm.io.out.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.file.SolidZipDataOutput;
import ru.olegcherednik.zip4jvm.io.out.file.SplitZipDataOutput;
import ru.olegcherednik.zip4jvm.io.writers.ExistedEntryWriter;
import ru.olegcherednik.zip4jvm.io.writers.entry.ZipEntryWriter;
import ru.olegcherednik.zip4jvm.model.ZipModel;
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/ru/olegcherednik/zip4jvm/io/ByteOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
*/
package ru.olegcherednik.zip4jvm.io;

import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.utils.BitUtils;
import ru.olegcherednik.zip4jvm.io.in.DataInput;
import ru.olegcherednik.zip4jvm.utils.ByteUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;

/**
* @author Oleg Cherednik
Expand All @@ -36,44 +37,54 @@ public enum ByteOrder {

@Override
public int readByte(DataInput in) throws IOException {
return BitUtils.readByte(in);
return ByteUtils.readByte(in);
}

@Override
public int readWord(DataInput in) throws IOException {
return BitUtils.readWord(in);
return ByteUtils.readWord(in);
}

@Override
public long readDword(byte[] buf, int offs) {
return ByteUtils.readDword(buf, offs);
}

@Override
public long readDword(DataInput in) throws IOException {
return BitUtils.readDword(in);
return ByteUtils.readDword(in);
}

@Override
public long readQword(DataInput in) throws IOException {
return BitUtils.readQword(in);
return ByteUtils.readQword(in);
}

@Override
public BigInteger readBigInteger(int size, DataInput in) throws IOException {
return ByteUtils.readBigInteger(size, in);
}

// ---------- write ----------

@Override
public void writeByte(int val, OutputStream out) throws IOException {
BitUtils.writeByte(val, out);
ByteUtils.writeByte(val, out);
}

@Override
public void writeWord(int val, OutputStream out) throws IOException {
BitUtils.writeWord(val, out);
ByteUtils.writeWord(val, out);
}

@Override
public void writeDword(long val, OutputStream out) throws IOException {
BitUtils.writeDword(val, out);
ByteUtils.writeDword(val, out);
}

@Override
public void writeQword(long val, OutputStream out) throws IOException {
BitUtils.writeQword(val, out);
ByteUtils.writeQword(val, out);
}

};
Expand All @@ -86,8 +97,12 @@ public void writeQword(long val, OutputStream out) throws IOException {

public abstract long readDword(DataInput in) throws IOException;

public abstract long readDword(byte[] buf, int offs);

public abstract long readQword(DataInput in) throws IOException;

public abstract BigInteger readBigInteger(int size, DataInput in) throws IOException;

// ---------- write ----------

public abstract void writeByte(int val, OutputStream out) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import ru.olegcherednik.zip4jvm.exception.Zip4jvmException;
import ru.olegcherednik.zip4jvm.io.in.BitInputStream;
import ru.olegcherednik.zip4jvm.io.in.data.DataInput;
import ru.olegcherednik.zip4jvm.io.in.DataInput;
import ru.olegcherednik.zip4jvm.model.Charsets;

import org.apache.commons.io.IOUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package ru.olegcherednik.zip4jvm.io.bzip2;

import ru.olegcherednik.zip4jvm.io.out.data.DataOutput;
import ru.olegcherednik.zip4jvm.io.out.DataOutput;
import ru.olegcherednik.zip4jvm.model.CompressionLevel;

import java.io.IOException;
Expand Down
Loading

0 comments on commit f8f6b6c

Please sign in to comment.