record size of encrypted temp data

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894303 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-10-16 10:30:43 +00:00
parent dfbab3ff6a
commit 0042a47172
5 changed files with 17 additions and 9 deletions

View File

@ -98,12 +98,9 @@ public final class EncryptedTempFilePackagePart extends PackagePart {
return tempFile.getOutputStream(); return tempFile.getOutputStream();
} }
/**
* @return EncryptedTempData.getSize() always returns -1
*/
@Override @Override
public long getSize() { public long getSize() {
return -1; return tempFile.getByteCount();
} }
@Override @Override

View File

@ -32,6 +32,7 @@ import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream; import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.io.output.CountingOutputStream;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.poi.poifs.crypt.ChainingMode; import org.apache.poi.poifs.crypt.ChainingMode;
@ -52,7 +53,8 @@ public class EncryptedTempData {
private final SecretKeySpec skeySpec; private final SecretKeySpec skeySpec;
private final byte[] ivBytes; private final byte[] ivBytes;
private final File tempFile; private final File tempFile;
private CountingOutputStream outputStream;
public EncryptedTempData() throws IOException { public EncryptedTempData() throws IOException {
SecureRandom sr = new SecureRandom(); SecureRandom sr = new SecureRandom();
ivBytes = new byte[16]; ivBytes = new byte[16];
@ -72,7 +74,8 @@ public class EncryptedTempData {
*/ */
public OutputStream getOutputStream() throws IOException { public OutputStream getOutputStream() throws IOException {
Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING); Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING);
return new CipherOutputStream(new FileOutputStream(tempFile), ciEnc); outputStream = new CountingOutputStream(new CipherOutputStream(new FileOutputStream(tempFile), ciEnc));
return outputStream;
} }
/** /**
@ -86,6 +89,13 @@ public class EncryptedTempData {
return new CipherInputStream(new FileInputStream(tempFile), ciDec); return new CipherInputStream(new FileInputStream(tempFile), ciDec);
} }
/**
* @return number of bytes stored in the temp data file (the number you should expect after you decrypt the data)
*/
public long getByteCount() {
return outputStream == null ? 0 : outputStream.getByteCount();
}
/** /**
* Removes the temporarily backing file * Removes the temporarily backing file
*/ */

View File

@ -33,15 +33,16 @@ public class TestEncryptedTempFilePackagePart {
@Test @Test
void testRoundTrip() throws Exception { void testRoundTrip() throws Exception {
String text = UUID.randomUUID().toString(); String text = UUID.randomUUID().toString();
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
try (OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ)) { try (OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ)) {
PackagePartName name = new PackagePartName("/test.txt", true); PackagePartName name = new PackagePartName("/test.txt", true);
EncryptedTempFilePackagePart part = new EncryptedTempFilePackagePart(p, name, "text/plain"); EncryptedTempFilePackagePart part = new EncryptedTempFilePackagePart(p, name, "text/plain");
try (OutputStream os = part.getOutputStream()) { try (OutputStream os = part.getOutputStream()) {
os.write(text.getBytes(StandardCharsets.UTF_8)); os.write(bytes);
} }
assertEquals(-1, part.getSize()); assertEquals(bytes.length, part.getSize());
try (InputStream is = part.getInputStream()) { try (InputStream is = part.getInputStream()) {
assertEquals(text, new String(IOUtils.toByteArray(is), StandardCharsets.UTF_8)); assertEquals(text, new String(IOUtils.toByteArray(is), StandardCharsets.UTF_8));
} }

View File

@ -40,7 +40,7 @@ public class TestTempFilePackagePart {
PackagePartName name = new PackagePartName("/test.txt", true); PackagePartName name = new PackagePartName("/test.txt", true);
TempFilePackagePart part = new TempFilePackagePart(p, name, "text/plain"); TempFilePackagePart part = new TempFilePackagePart(p, name, "text/plain");
try (OutputStream os = part.getOutputStream()) { try (OutputStream os = part.getOutputStream()) {
os.write(text.getBytes(StandardCharsets.UTF_8)); os.write(bytes);
} }
assertEquals(bytes.length, part.getSize()); assertEquals(bytes.length, part.getSize());
try (InputStream is = part.getInputStream()) { try (InputStream is = part.getInputStream()) {

Binary file not shown.