cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1586980 - in /cayenne/main/trunk/cayenne-crypto/src: main/java/org/apache/cayenne/crypto/transformer/bytes/ test/java/org/apache/cayenne/crypto/transformer/bytes/
Date Sun, 13 Apr 2014 13:28:44 GMT
Author: aadamchik
Date: Sun Apr 13 13:28:44 2014
New Revision: 1586980

URL: http://svn.apache.org/r1586980
Log:
CAY-1925 cayenne-crypto: add optional compression to the encryption pipeline

using the new Header class in byte decryptors

Added:
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderTest.java
      - copied, changed from r1586979, cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptorTest.java
Removed:
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptorTest.java
Modified:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptor.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.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=1586980&r1=1586979&r2=1586980&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 13:28:44 2014
@@ -98,7 +98,7 @@ class CbcBytesTransformerFactory impleme
         BytesDecryptor cbcDecryptor = new CbcDecryptor(cipher);
 
         // TODO: make checking for key name an optional property
-        return new HeaderDecryptor(cbcDecryptor, keySource, blockSize);
+        return new HeaderDecryptor(cbcDecryptor, keySource);
     }
 
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java?rev=1586980&r1=1586979&r2=1586980&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java
Sun Apr 13 13:28:44 2014
@@ -53,6 +53,7 @@ public class Header {
     public static final int FLAGS_OFFSET = 0;
 
     private byte[] data;
+    private int offset;
 
     public static Header create(String keyName) {
         byte[] keyNameBytes;
@@ -71,27 +72,35 @@ public class Header {
                     + "' is too long. Its UTF8-encoded form should not exceed " + KEY_NAME_SIZE
+ " bytes");
         }
 
-        return create(data);
+        return create(data, 0);
     }
 
-    public static Header create(byte[] data) {
+    public static Header create(byte[] data, int offset) {
 
-        if (data.length != HEADER_SIZE) {
+        if (data.length - offset < HEADER_SIZE) {
             throw new CayenneCryptoException("Unexpected header data size: " + data.length
+ ", expected size is "
                     + HEADER_SIZE);
         }
 
-        Header h = new Header();
-        h.data = data;
-        return h;
+        return new Header(data, offset);
     }
 
     // private constructor... construction is done via factory methods...
-    private Header() {
+    private Header(byte[] data, int offset) {
+        this.data = data;
+        this.offset = offset;
+    }
 
+    public void store(byte[] output, int outputOffset) {
+        System.arraycopy(data, offset, output, outputOffset, Header.HEADER_SIZE);
     }
 
-    public byte[] getData() {
-        return data;
+    public String getKeyName() {
+        try {
+            // 'trim' is to get rid of 0 padding
+            return new String(data, offset + KEY_NAME_OFFSET, KEY_NAME_SIZE, KEY_NAME_CHARSET).trim();
+        } catch (UnsupportedEncodingException e) {
+            throw new CayenneCryptoException("Can't decode with " + KEY_NAME_CHARSET, e);
+        }
     }
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptor.java?rev=1586980&r1=1586979&r2=1586980&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptor.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptor.java
Sun Apr 13 13:28:44 2014
@@ -18,10 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.bytes;
 
-import java.io.UnsupportedEncodingException;
 import java.security.Key;
 
-import org.apache.cayenne.crypto.CayenneCryptoException;
 import org.apache.cayenne.crypto.key.KeySource;
 
 /**
@@ -29,15 +27,11 @@ import org.apache.cayenne.crypto.key.Key
  */
 class HeaderDecryptor implements BytesDecryptor {
 
-    private static final String KEY_NAME_CHARSET = "UTF-8";
-
     private KeySource keySource;
     private BytesDecryptor delegate;
-    private int blockSize;
 
-    public HeaderDecryptor(BytesDecryptor delegate, KeySource keySource, int blockSize) {
+    public HeaderDecryptor(BytesDecryptor delegate, KeySource keySource) {
         this.delegate = delegate;
-        this.blockSize = blockSize;
         this.keySource = keySource;
     }
 
@@ -46,18 +40,13 @@ class HeaderDecryptor implements BytesDe
 
         // ignoring the parameter key... using the key from the first block
 
-        String keyName = keyName(input, inputOffset);
-        Key inRecordKey = keySource.getKey(keyName);
-        return delegate.decrypt(input, inputOffset + blockSize, inRecordKey);
+        Header header = header(input, inputOffset);
+        Key inRecordKey = keySource.getKey(header.getKeyName());
+        return delegate.decrypt(input, inputOffset + Header.HEADER_SIZE, inRecordKey);
     }
 
-    String keyName(byte[] input, int inputOffset) {
-        try {
-            // 'trim' is to get rid of 0 padding
-            return new String(input, inputOffset, blockSize, KEY_NAME_CHARSET).trim();
-        } catch (UnsupportedEncodingException e) {
-            throw new CayenneCryptoException("Can't decode with " + KEY_NAME_CHARSET, e);
-        }
+    Header header(byte[] input, int inputOffset) {
+        return Header.create(input, inputOffset);
     }
 
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.java?rev=1586980&r1=1586979&r2=1586980&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.java
Sun Apr 13 13:28:44 2014
@@ -31,7 +31,7 @@ class HeaderEncryptor implements BytesEn
     @Override
     public byte[] encrypt(byte[] input, int outputOffset) {
         byte[] output = delegate.encrypt(input, outputOffset + Header.HEADER_SIZE);
-        System.arraycopy(header.getData(), 0, output, outputOffset, Header.HEADER_SIZE);
+        header.store(output, outputOffset);
         return output;
     }
 

Copied: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderTest.java
(from r1586979, cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptorTest.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderTest.java?p2=cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderTest.java&p1=cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptorTest.java&r1=1586979&r2=1586980&rev=1586980&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderDecryptorTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderTest.java
Sun Apr 13 13:28:44 2014
@@ -19,25 +19,29 @@
 package org.apache.cayenne.crypto.transformer.bytes;
 
 import static org.junit.Assert.assertEquals;
-import static org.mockito.Mockito.mock;
 
-import org.apache.cayenne.crypto.key.KeySource;
+import org.apache.cayenne.crypto.CayenneCryptoException;
 import org.junit.Test;
 
-public class HeaderDecryptorTest {
+public class HeaderTest {
 
     @Test
-    public void testKeyName() {
+    public void testCreate_WithKeyName() {
 
-        byte[] input1 = { 'a', 'b', 'c', 'd', 'e' };
-        byte[] input2 = { 'a', 'b', 'c', 0, 'e' };
-        byte[] input3 = { 'a', 'b', 0, 0, 'e' };
-
-        HeaderDecryptor decryptor = new HeaderDecryptor(mock(BytesDecryptor.class), mock(KeySource.class),
3);
-        assertEquals("bcd", decryptor.keyName(input1, 1));
-        assertEquals("bc", decryptor.keyName(input2, 1));
-        assertEquals("b", decryptor.keyName(input3, 1));
+        assertEquals("bcd", Header.create("bcd").getKeyName());
+        assertEquals("bc", Header.create("bc").getKeyName());
+        assertEquals("b", Header.create("b").getKeyName());
+    }
+
+    @Test
+    public void testCreate_WithData() {
+        byte[] input1 = { 0, 0, 0, 0, 0, 0, 0, 0, 'a', 'b', 'c', 'd', 'e', 0, 0, 0, 0 };
+        assertEquals("bcde", Header.create(input1, 1).getKeyName());
+    }
 
+    @Test(expected = CayenneCryptoException.class)
+    public void testCreate_WithData_WrongLength() {
+        Header.create(new byte[] { 'a', 'b', 0, 0, 'e' }, 0);
     }
 
 }



Mime
View raw message