You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2014/04/13 21:48:43 UTC
svn commit: r1587084 - in /cayenne/main/trunk/cayenne-crypto/src:
main/java/org/apache/cayenne/crypto/transformer/bytes/
test/java/org/apache/cayenne/crypto/transformer/bytes/
Author: aadamchik
Date: Sun Apr 13 19:48:42 2014
New Revision: 1587084
URL: http://svn.apache.org/r1587084
Log:
CAY-1925 cayenne-crypto: add optional compression to the encryption pipeline
actual compression code
Added:
cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptor.java
cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptorTest.java
Modified:
cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java
Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java?rev=1587084&r1=1587083&r2=1587084&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java (original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java Sun Apr 13 19:48:42 2014
@@ -88,8 +88,13 @@ class CbcBytesTransformerFactory impleme
@Override
public BytesEncryptor encryptor() {
Cipher cipher = cipherFactory.cipher();
- BytesEncryptor cbcEncryptor = new CbcEncryptor(cipher, key, generateSeedIv());
- return new HeaderEncryptor(cbcEncryptor, encryptionHeader);
+ BytesEncryptor delegate = new CbcEncryptor(cipher, key, generateSeedIv());
+
+ if (encryptionHeader.isCompressed()) {
+ delegate = new GzipEncryptor(delegate, encryptionHeader);
+ }
+
+ return new HeaderEncryptor(delegate, encryptionHeader);
}
@Override
Added: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptor.java?rev=1587084&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptor.java (added)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptor.java Sun Apr 13 19:48:42 2014
@@ -0,0 +1,67 @@
+/*****************************************************************
+ * 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.cayenne.crypto.transformer.bytes;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.zip.GZIPOutputStream;
+
+import org.apache.cayenne.crypto.CayenneCryptoException;
+
+/**
+ * @since 3.2
+ */
+class GzipEncryptor implements BytesEncryptor {
+
+ private BytesEncryptor delegate;
+ private Header header;
+
+ public GzipEncryptor(BytesEncryptor delegate, Header header) {
+ this.delegate = delegate;
+ this.header = header;
+ }
+
+ @Override
+ public byte[] encrypt(byte[] input, int outputOffset) {
+
+ // TODO: skip compression of small inputs... somehow flip compression
+ // bit in the header back to off in that case.
+
+ byte[] compressedIn;
+ try {
+ compressedIn = gzip(input);
+ } catch (IOException e) {
+ // really not expecting an error here...
+ throw new CayenneCryptoException("Error compressing input", e);
+ }
+
+ return delegate.encrypt(compressedIn, outputOffset + header.size());
+ }
+
+ static byte[] gzip(byte[] input) throws IOException {
+ ByteArrayOutputStream zipBytes = new ByteArrayOutputStream(input.length);
+ GZIPOutputStream out = new GZIPOutputStream(zipBytes);
+
+ out.write(input, 0, input.length);
+ out.close();
+
+ return zipBytes.toByteArray();
+ }
+
+}
Added: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptorTest.java?rev=1587084&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptorTest.java (added)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/GzipEncryptorTest.java Sun Apr 13 19:48:42 2014
@@ -0,0 +1,77 @@
+/*****************************************************************
+ * 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.cayenne.crypto.transformer.bytes;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.cayenne.crypto.unit.CryptoUnitUtils;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class GzipEncryptorTest {
+
+ @Test
+ public void testGzip() throws IOException {
+
+ byte[] input1 = "Hello Hello Hello".getBytes("UTF8");
+ byte[] output1 = GzipEncryptor.gzip(input1);
+ byte[] expectedOutput1 = CryptoUnitUtils.hexToBytes("1f8b0800000000000000f348cdc9c957f0409000a91a078c11000000");
+ assertArrayEquals(expectedOutput1, output1);
+ }
+
+ @Test
+ public void testEncrypt() throws UnsupportedEncodingException {
+
+ final Header header = Header.create("kk", true);
+ BytesEncryptor delegate = mock(BytesEncryptor.class);
+ when(delegate.encrypt(any(byte[].class), anyInt())).thenAnswer(new Answer<byte[]>() {
+ @Override
+ public byte[] answer(InvocationOnMock invocation) throws Throwable {
+ Object[] args = invocation.getArguments();
+
+ byte[] answer = (byte[]) args[0];
+ int offset = (Integer) args[1];
+
+ assertEquals(header.size(), offset);
+
+ return answer;
+ }
+ });
+
+
+ GzipEncryptor e = new GzipEncryptor(delegate, header);
+
+ byte[] input1 = "Hello Hello Hello".getBytes("UTF8");
+ byte[] output1 = e.encrypt(input1, 0);
+ byte[] expectedOutput1 = CryptoUnitUtils.hexToBytes("1f8b0800000000000000f348cdc9c957f0409000a91a078c11000000");
+
+ assertArrayEquals(expectedOutput1, output1);
+
+ }
+
+}