cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1584626 - in /cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes: CbcDecryptorTest.java CbcEncryptorTest.java
Date Fri, 04 Apr 2014 12:03:21 GMT
Author: aadamchik
Date: Fri Apr  4 12:03:21 2014
New Revision: 1584626

URL: http://svn.apache.org/r1584626
Log:
CAY-1916 cayenne-crypto module that enables data encryption for certain model attributes

BytesEncryptor/BytesDecryptor to encapsulate crypto protocol .. IN PROGRESS

Modified:
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java

Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java?rev=1584626&r1=1584625&r2=1584626&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java
Fri Apr  4 12:03:21 2014
@@ -21,12 +21,10 @@ package org.apache.cayenne.crypto.transf
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
-import java.math.BigInteger;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
 
 import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
@@ -35,6 +33,7 @@ import javax.crypto.NoSuchPaddingExcepti
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 
+import org.apache.cayenne.crypto.unit.CryptoTestUtils;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -43,20 +42,9 @@ public class CbcDecryptorTest {
     private Cipher cipher;
     private Key key;
 
-    private byte[] hex(String hexString) {
-        byte[] bytes = new BigInteger(hexString, 16).toByteArray();
-
-        // http://stackoverflow.com/questions/4407779/biginteger-to-byte
-        if (bytes.length > 0 && bytes[0] == 0) {
-            return Arrays.copyOfRange(bytes, 1, bytes.length);
-        } else {
-            return bytes;
-        }
-    }
-
     @Before
     public void before() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException
{
-        byte[] keyBytes = hex("a4cb499fa31a6a228e16b7e4741d4fa3");
+        byte[] keyBytes = CryptoTestUtils.hexToBytes("a4cb499fa31a6a228e16b7e4741d4fa3");
         this.key = new SecretKeySpec(keyBytes, "AES");
 
         this.cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
@@ -82,7 +70,7 @@ public class CbcDecryptorTest {
         CbcDecryptor decryptor = new CbcDecryptor(cipher);
 
         byte[] plain = { 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
3, 2, 1 };
-        byte[] ivBytes = hex("0591849d87c93414f4405d32f4d69220");
+        byte[] ivBytes = CryptoTestUtils.hexToBytes("0591849d87c93414f4405d32f4d69220");
 
         Cipher encCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
         encCipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(ivBytes));

Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java?rev=1584626&r1=1584625&r2=1584626&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java
Fri Apr  4 12:03:21 2014
@@ -31,6 +31,7 @@ import javax.crypto.NoSuchPaddingExcepti
 import javax.crypto.spec.SecretKeySpec;
 
 import org.apache.cayenne.crypto.CayenneCryptoException;
+import org.apache.cayenne.crypto.unit.CryptoTestUtils;
 import org.junit.Test;
 
 public class CbcEncryptorTest {
@@ -48,7 +49,7 @@ public class CbcEncryptorTest {
     }
 
     @Test
-    public void testGetOutputSize() throws UnsupportedEncodingException, NoSuchAlgorithmException,
+    public void testGetOutputSize_DES() throws UnsupportedEncodingException, NoSuchAlgorithmException,
             NoSuchPaddingException, InvalidKeyException {
 
         byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
@@ -64,4 +65,27 @@ public class CbcEncryptorTest {
         assertEquals(24, encryptor.getOutputSize(11));
     }
 
+    @Test
+    public void testGetOutputSize_AES() throws UnsupportedEncodingException, NoSuchAlgorithmException,
+            NoSuchPaddingException, InvalidKeyException {
+
+        byte[] ivBytes = CryptoTestUtils.hexToBytes("0591849d87c93414f4405d32f4d69220");
+        byte[] keyBytes = CryptoTestUtils.hexToBytes("a4cb499fa31a6a228e16b7e4741d4fa3");
+        Key key = new SecretKeySpec(keyBytes, "AES");
+
+        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+        cipher.init(Cipher.ENCRYPT_MODE, key);
+        assertEquals(16, cipher.getBlockSize());
+
+        // try with non-standard block size too...
+        CbcEncryptor encryptor = new CbcEncryptor(cipher, key, ivBytes);
+        assertEquals(32, encryptor.getOutputSize(11));
+    }
+
+    @Test
+    public void testEncrypt() {
+
+        // CbcEncryptor encryptor = new CbcEncryptor(cipher, key, iv);
+    }
+
 }



Mime
View raw message