You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2013/10/15 11:37:27 UTC
svn commit: r1532264 - in /commons/proper/compress/trunk/src:
main/java/org/apache/commons/compress/archivers/sevenz/
test/java/org/apache/commons/compress/archivers/sevenz/ test/resources/
Author: bodewig
Date: Tue Oct 15 09:37:27 2013
New Revision: 1532264
URL: http://svn.apache.org/r1532264
Log:
password String => byte[] in SevnZFile including test for decryption
Added:
commons/proper/compress/trunk/src/test/resources/bla.encrypted.7z (with props)
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java?rev=1532264&r1=1532263&r2=1532264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java Tue Oct 15 09:37:27 2013
@@ -42,7 +42,7 @@ import org.tukaani.xz.LZMAInputStream;
class Coders {
static InputStream addDecoder(final InputStream is,
- final Coder coder, final String password) throws IOException {
+ final Coder coder, final byte[] password) throws IOException {
for (final CoderId coderId : coderTable) {
if (Arrays.equals(coderId.method.getId(), coder.decompressionMethodId)) {
return coderId.coder.decode(is, coder, password);
@@ -53,7 +53,7 @@ class Coders {
}
static OutputStream addEncoder(final OutputStream out, final SevenZMethod method,
- final String password) throws IOException {
+ final byte[] password) throws IOException {
for (final CoderId coderId : coderTable) {
if (coderId.method.equals(method)) {
return coderId.coder.encode(out, password);
@@ -83,8 +83,8 @@ class Coders {
static abstract class CoderBase {
abstract InputStream decode(final InputStream in, final Coder coder,
- String password) throws IOException;
- OutputStream encode(final OutputStream out, final String password)
+ byte[] password) throws IOException;
+ OutputStream encode(final OutputStream out, final byte[] password)
throws IOException {
throw new UnsupportedOperationException("method doesn't support writing");
}
@@ -93,11 +93,11 @@ class Coders {
static class CopyDecoder extends CoderBase {
@Override
InputStream decode(final InputStream in, final Coder coder,
- String password) throws IOException {
+ byte[] password) throws IOException {
return in;
}
@Override
- OutputStream encode(final OutputStream out, final String password) {
+ OutputStream encode(final OutputStream out, final byte[] password) {
return out;
}
}
@@ -105,7 +105,7 @@ class Coders {
static class LZMADecoder extends CoderBase {
@Override
InputStream decode(final InputStream in, final Coder coder,
- String password) throws IOException {
+ byte[] password) throws IOException {
byte propsByte = coder.properties[0];
long dictSize = coder.properties[1];
for (int i = 1; i < 4; i++) {
@@ -120,25 +120,25 @@ class Coders {
static class DeflateDecoder extends CoderBase {
@Override
- InputStream decode(final InputStream in, final Coder coder, final String password)
+ InputStream decode(final InputStream in, final Coder coder, final byte[] password)
throws IOException {
return new InflaterInputStream(new DummyByteAddingInputStream(in),
new Inflater(true));
}
@Override
- OutputStream encode(final OutputStream out, final String password) {
+ OutputStream encode(final OutputStream out, final byte[] password) {
return new DeflaterOutputStream(out, new Deflater(9, true));
}
}
static class BZIP2Decoder extends CoderBase {
@Override
- InputStream decode(final InputStream in, final Coder coder, final String password)
+ InputStream decode(final InputStream in, final Coder coder, final byte[] password)
throws IOException {
return new BZip2CompressorInputStream(in);
}
@Override
- OutputStream encode(final OutputStream out, final String password)
+ OutputStream encode(final OutputStream out, final byte[] password)
throws IOException {
return new BZip2CompressorOutputStream(out);
}
@@ -147,7 +147,7 @@ class Coders {
static class AES256SHA256Decoder extends CoderBase {
@Override
InputStream decode(final InputStream in, final Coder coder,
- final String password) throws IOException {
+ final byte[] passwordBytes) throws IOException {
return new InputStream() {
private boolean isInitialized = false;
private CipherInputStream cipherInputStream = null;
@@ -170,10 +170,9 @@ class Coders {
final byte[] iv = new byte[16];
System.arraycopy(coder.properties, 2 + saltSize, iv, 0, ivSize);
- if (password == null) {
+ if (passwordBytes == null) {
throw new IOException("Cannot read encrypted files without a password");
}
- final byte[] passwordBytes = password.getBytes("UTF-16LE");
final byte[] aesKeyBytes;
if (numCyclesPower == 0x3f) {
aesKeyBytes = new byte[32];
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java?rev=1532264&r1=1532263&r2=1532264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java Tue Oct 15 09:37:27 2013
@@ -28,7 +28,7 @@ import org.tukaani.xz.LZMA2Options;
class LZMA2Decoder extends Coders.CoderBase {
@Override
- InputStream decode(final InputStream in, final Coder coder, String password)
+ InputStream decode(final InputStream in, final Coder coder, byte[] password)
throws IOException {
final int dictionarySizeBits = 0xff & coder.properties[0];
if ((dictionarySizeBits & (~0x3f)) != 0) {
@@ -47,7 +47,7 @@ class LZMA2Decoder extends Coders.CoderB
}
@Override
- OutputStream encode(final OutputStream out, final String password)
+ OutputStream encode(final OutputStream out, final byte[] password)
throws IOException {
LZMA2Options options = new LZMA2Options();
options.setDictSize(LZMA2Options.DICT_SIZE_DEFAULT);
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java?rev=1532264&r1=1532263&r2=1532264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java Tue Oct 15 09:37:27 2013
@@ -70,7 +70,7 @@ public class SevenZFile {
private int currentFolderIndex = -1;
private InputStream currentFolderInputStream = null;
private InputStream currentEntryInputStream = null;
- private String password;
+ private byte[] password;
static final byte[] sevenZSignature = {
(byte)'7', (byte)'z', (byte)0xBC, (byte)0xAF, (byte)0x27, (byte)0x1C
@@ -80,14 +80,16 @@ public class SevenZFile {
* Reads a file as 7z archive
*
* @param filename the file to read
- * @param password optional password if the archive is encrypted
+ * @param password optional password if the archive is encrypted -
+ * the byte array is supposed to be the UTF16-LE encoded
+ * representation of the password.
*/
- public SevenZFile(final File filename, final String password) throws IOException {
+ public SevenZFile(final File filename, final byte[] password) throws IOException {
boolean succeeded = false;
- this.password = password;
this.file = new RandomAccessFile(filename, "r");
try {
- archive = readHeaders();
+ archive = readHeaders(password);
+ this.password = password;
succeeded = true;
} finally {
if (!succeeded) {
@@ -147,7 +149,7 @@ public class SevenZFile {
return entry;
}
- private Archive readHeaders() throws IOException {
+ private Archive readHeaders(byte[] password) throws IOException {
debug("SignatureHeader");
final byte[] signature = new byte[6];
@@ -187,7 +189,8 @@ public class SevenZFile {
Archive archive = new Archive();
int nid = nextHeaderInputStream.readUnsignedByte();
if (nid == NID.kEncodedHeader) {
- nextHeaderInputStream = readEncodedHeader(nextHeaderInputStream, archive);
+ nextHeaderInputStream =
+ readEncodedHeader(nextHeaderInputStream, archive, password);
// Archive gets rebuilt with the new header
archive = new Archive();
nid = nextHeaderInputStream.readUnsignedByte();
@@ -260,7 +263,8 @@ public class SevenZFile {
}
}
- private DataInputStream readEncodedHeader(final DataInputStream header, final Archive archive) throws IOException {
+ private DataInputStream readEncodedHeader(final DataInputStream header, final Archive archive,
+ byte[] password) throws IOException {
debug("EncodedHeader");
readStreamsInfo(header, archive);
Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java?rev=1532264&r1=1532263&r2=1532264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java Tue Oct 15 09:37:27 2013
@@ -49,8 +49,16 @@ public class SevenZFileTest extends Abst
test7zUnarchive(getFile("bla.deflate.7z"));
}
+ public void test7zDecryptUnarchive() throws Exception {
+ test7zUnarchive(getFile("bla.encrypted.7z"), "foo".getBytes("UTF-16LE"));
+ }
+
private void test7zUnarchive(File f) throws Exception {
- SevenZFile sevenZFile = new SevenZFile(f);
+ test7zUnarchive(f, null);
+ }
+
+ private void test7zUnarchive(File f, byte[] password) throws Exception {
+ SevenZFile sevenZFile = new SevenZFile(f, password);
try {
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
assertEquals("test1.xml", entry.getName());
Added: commons/proper/compress/trunk/src/test/resources/bla.encrypted.7z
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/resources/bla.encrypted.7z?rev=1532264&view=auto
==============================================================================
Binary file - no diff available.
Propchange: commons/proper/compress/trunk/src/test/resources/bla.encrypted.7z
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream