Skip to content

Commit

Permalink
feat(encrypt): hack compress encrypted with password
Browse files Browse the repository at this point in the history
..as it not supported by Apache Commons Compress

Next step, PR to Apache Commons Compress project

#1
  • Loading branch information
Dougniel committed Nov 25, 2022
1 parent ed5254f commit 28e8666
Show file tree
Hide file tree
Showing 58 changed files with 9,273 additions and 11 deletions.
10 changes: 3 additions & 7 deletions src/main/java/nc/opt/util/J7zip.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,18 @@ public Integer call() throws Exception {

J7zip.decompress(names[1], names[2], password, command == Command.x);
} else if (command == Command.a) {
if (password != null) {
System.err.println("password protection is only applicable on decompression");
return 1;
}
if (names.length < 2) {
System.err.println("Destination archive file name or source dir(s)/file(s) missing");
return 1;
}

J7zip.compress(names[1], Stream.of(names).skip(2).map(File::new).toArray(File[]::new));
J7zip.compress(names[1], password, Stream.of(names).skip(2).map(File::new).toArray(File[]::new));
}
return 0;
}

public static void compress(String name, File... files) throws IOException {
try (SevenZOutputFile out = new SevenZOutputFile(new File(name))) {
public static void compress(String name, String password, File... files) throws IOException {
try (SevenZOutputFile out = new SevenZOutputFile(new File(name), password != null ? password.toCharArray() : null)) {
for (File file : files) {
addToArchiveCompression(out, file, file.getParent());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.compress;

import java.io.IOException;

/**
* If a stream checks for estimated memory allocation, and the estimate
* goes above the memory limit, this is thrown. This can also be thrown
* if a stream tries to allocate a byte array that is larger than
* the allowable limit.
*
* @since 1.14
*/
public class MemoryLimitException extends IOException {

private static final long serialVersionUID = 1L;

//long instead of int to account for overflow for corrupt files
private final long memoryNeededInKb;
private final int memoryLimitInKb;

public MemoryLimitException(final long memoryNeededInKb, final int memoryLimitInKb) {
super(buildMessage(memoryNeededInKb, memoryLimitInKb));
this.memoryNeededInKb = memoryNeededInKb;
this.memoryLimitInKb = memoryLimitInKb;
}

public MemoryLimitException(final long memoryNeededInKb, final int memoryLimitInKb, final Exception e) {
super(buildMessage(memoryNeededInKb, memoryLimitInKb), e);
this.memoryNeededInKb = memoryNeededInKb;
this.memoryLimitInKb = memoryLimitInKb;
}

public long getMemoryNeededInKb() {
return memoryNeededInKb;
}

public int getMemoryLimitInKb() {
return memoryLimitInKb;
}

private static String buildMessage(final long memoryNeededInKb, final int memoryLimitInKb) {
return memoryNeededInKb + " kb of memory would be needed; limit was "
+ memoryLimitInKb + " kb. " +
"If the file is not corrupt, consider increasing the memory limit.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.compress;

import java.io.IOException;

/**
* Exception thrown when trying to read an encrypted entry or file without
* configuring a password.
* @since 1.10
*/
public class PasswordRequiredException extends IOException {

private static final long serialVersionUID = 1391070005491684483L;

/**
* Create a new exception.
*
* @param name name of the archive containing encrypted streams or
* the encrypted file.
*/
public PasswordRequiredException(final String name) {
super("Cannot read encrypted content from " + name + " without a password.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.compress.archivers;

import java.util.Date;

/**
* Represents an entry of an archive.
*/
public interface ArchiveEntry {

/**
* Gets the name of the entry in this archive. May refer to a file or directory or other item.
*
* <p>This method returns the raw name as it is stored inside of the archive.</p>
*
* @return The name of this entry in the archive.
*/
String getName();

/**
* Gets the uncompressed size of this entry. May be -1 (SIZE_UNKNOWN) if the size is unknown
*
* @return the uncompressed size of this entry.
*/
long getSize();

/** Special value indicating that the size is unknown */
long SIZE_UNKNOWN = -1;

/**
* Returns true if this entry refers to a directory.
*
* @return true if this entry refers to a directory.
*/
boolean isDirectory();

/**
* Gets the last modified date of this entry.
*
* @return the last modified date of this entry.
* @since 1.1
*/
Date getLastModifiedDate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.apache.commons.compress.archivers.sevenz;

import java.util.Arrays;
import java.util.Random;

public class AES256Options {
byte[] password;
byte[] salt = new byte[0];
byte[] iv = new byte[16];
int numCyclesPower = 19;

public AES256Options(byte[] password) {
this.password = password;
new Random(Arrays.hashCode(password)).nextBytes(salt);
new Random(Arrays.hashCode(password)).nextBytes(iv);
}
}
Loading

0 comments on commit 28e8666

Please sign in to comment.