Search in sources :

Example 1 with CipherTransformer

use of org.syncany.chunk.CipherTransformer in project syncany by syncany.

the class FrameworkCombinationTest method fillCombinations.

private void fillCombinations() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
    // MultiChunks
    List<MultiChunker> multiChunkers = new LinkedList<MultiChunker>();
    int[] multiChunkSizes = new int[] { 250000, 500000 };
    for (int i = 0; i < multiChunkSizes.length; i++) {
        //multiChunkers.add(new CustomMultiChunker(multiChunkSizes[i]));
        multiChunkers.add(new ZipMultiChunker(multiChunkSizes[i]));
    }
    // Chunks
    List<Chunker> chunkers = new LinkedList<Chunker>();
    int[] chunkSizes = new int[] { 8000, 16000 };
    String[] digestAlgs = new String[] { /*"MD5" ,*/
    "SHA1" };
    String[] fingerprinters = new String[] { "Adler32" };
    for (int i = 0; i < chunkSizes.length; i++) {
        for (int j = 0; j < digestAlgs.length; j++) {
            for (int k = 0; k < fingerprinters.length; k++) {
                chunkers.add(new TttdChunker(chunkSizes[i], TttdChunker.DEFAULT_WINDOW_SIZE, digestAlgs[j], fingerprinters[k]));
            }
        }
    }
    // Compression/Encryption
    List<CipherSpec> cipherSpecs = new ArrayList<CipherSpec>();
    cipherSpecs.add(CipherSpecs.getCipherSpec(1));
    cipherSpecs.add(CipherSpecs.getCipherSpec(2));
    List<Transformer> transformerChains = new LinkedList<Transformer>();
    transformerChains.add(new NoTransformer());
    transformerChains.add(new GzipTransformer());
    transformerChains.add(new CipherTransformer(cipherSpecs, masterKey));
    transformerChains.add(new GzipTransformer(new CipherTransformer(cipherSpecs, masterKey)));
    for (MultiChunker multiChunker : multiChunkers) {
        for (Transformer transformer : transformerChains) {
            for (Chunker chunker : chunkers) {
                String configName = multiChunker + "/" + chunker + "/" + transformer;
                combinations.add(new FrameworkCombination(configName, chunker, multiChunker, transformer));
            }
        }
    }
}
Also used : GzipTransformer(org.syncany.chunk.GzipTransformer) CipherTransformer(org.syncany.chunk.CipherTransformer) GzipTransformer(org.syncany.chunk.GzipTransformer) NoTransformer(org.syncany.chunk.NoTransformer) Transformer(org.syncany.chunk.Transformer) ZipMultiChunker(org.syncany.chunk.ZipMultiChunker) TttdChunker(org.syncany.chunk.TttdChunker) Chunker(org.syncany.chunk.Chunker) MultiChunker(org.syncany.chunk.MultiChunker) CipherTransformer(org.syncany.chunk.CipherTransformer) ArrayList(java.util.ArrayList) ZipMultiChunker(org.syncany.chunk.ZipMultiChunker) MultiChunker(org.syncany.chunk.MultiChunker) TttdChunker(org.syncany.chunk.TttdChunker) LinkedList(java.util.LinkedList) NoTransformer(org.syncany.chunk.NoTransformer) ZipMultiChunker(org.syncany.chunk.ZipMultiChunker) CipherSpec(org.syncany.crypto.CipherSpec)

Example 2 with CipherTransformer

use of org.syncany.chunk.CipherTransformer in project syncany by syncany.

the class MultiCipherStreamsTest method testSaxParserWithMultiCipherTransformer.

public void testSaxParserWithMultiCipherTransformer(List<CipherSpec> cipherSuites) throws Exception {
    String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<database version=\"1\">\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "</database>";
    Transformer cipherTransformer = new CipherTransformer(cipherSuites, masterKey);
    // Test encrypt
    byte[] encryptedData = doEncrypt(StringUtil.toBytesUTF8(xmlStr), cipherTransformer);
    // Test decrypt with SAX parser	
    InputStream is = cipherTransformer.createInputStream(new ByteArrayInputStream(encryptedData));
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    saxParser.parse(is, new DefaultHandler());
// Success if it does not throw an exception
// Regular CipherInputStream does NOT work with GCM mode
// GcmCompatibleCipherInputStream fixes this!
// See http://bouncy-castle.1462172.n4.nabble.com/Using-AES-GCM-NoPadding-with-javax-crypto-CipherInputStream-td4655271.html
// and http://bouncy-castle.1462172.n4.nabble.com/using-GCMBlockCipher-with-CipherInputStream-td4655147.html
}
Also used : CipherTransformer(org.syncany.chunk.CipherTransformer) Transformer(org.syncany.chunk.Transformer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CipherTransformer(org.syncany.chunk.CipherTransformer) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 3 with CipherTransformer

use of org.syncany.chunk.CipherTransformer in project syncany by syncany.

the class MultiCipherStreamsTest method doTestEncryption.

private void doTestEncryption(List<CipherSpec> cipherSpecs) throws InvalidKeySpecException, NoSuchAlgorithmException, IOException, CipherException, InvalidKeyException {
    Transformer encryptCipherTransformer = new CipherTransformer(cipherSpecs, masterKey);
    Transformer decryptCipherTransformer = new CipherTransformer(cipherSpecs, masterKey);
    // Prepare data
    byte[] srcData = new byte[10 * 1024];
    for (int i = 0; i < srcData.length; i++) {
        srcData[i] = (byte) (i & 0xff);
    }
    byte[] encryptedData1 = doEncrypt(srcData, encryptCipherTransformer);
    logger.log(Level.INFO, "Encrypted Data (Round 1): " + StringUtil.toHex(encryptedData1));
    byte[] decryptedData1 = doDecrypt(encryptedData1, decryptCipherTransformer);
    byte[] encryptedData2 = doEncrypt(srcData, encryptCipherTransformer);
    byte[] decryptedData2 = doDecrypt(encryptedData2, decryptCipherTransformer);
    logger.log(Level.INFO, "Source Data:              " + StringUtil.toHex(srcData));
    logger.log(Level.INFO, "Decrypted Data (Round 1): " + StringUtil.toHex(decryptedData1));
    logger.log(Level.INFO, "Decrypted Data (Round 2): " + StringUtil.toHex(decryptedData2));
    logger.log(Level.INFO, "Encrypted Data (Round 1): " + StringUtil.toHex(encryptedData1));
    logger.log(Level.INFO, "Encrypted Data (Round 2): " + StringUtil.toHex(encryptedData2));
    assertEquals("Source data and decrypted data is different (round 1)", StringUtil.toHex(srcData), StringUtil.toHex(decryptedData1));
    assertEquals("Source data and decrypted data is different (round 2)", StringUtil.toHex(srcData), StringUtil.toHex(decryptedData2));
    assertNotSame("Encrypted data for round 1 and 2 are identical", StringUtil.toHex(encryptedData1), StringUtil.toHex(encryptedData2));
    logger.log(Level.INFO, "Passed.");
}
Also used : CipherTransformer(org.syncany.chunk.CipherTransformer) Transformer(org.syncany.chunk.Transformer) CipherTransformer(org.syncany.chunk.CipherTransformer)

Example 4 with CipherTransformer

use of org.syncany.chunk.CipherTransformer in project syncany by syncany.

the class Config method initTransformers.

private void initTransformers(RepoTO repoTO) throws Exception {
    if (repoTO.getTransformers() == null || repoTO.getTransformers().size() == 0) {
        transformer = new NoTransformer();
    } else {
        List<TransformerTO> transformerTOs = new ArrayList<TransformerTO>(repoTO.getTransformers());
        Transformer lastTransformer = null;
        for (int i = transformerTOs.size() - 1; i >= 0; i--) {
            TransformerTO transformerTO = transformerTOs.get(i);
            Transformer transformer = Transformer.getInstance(transformerTO.getType());
            if (transformer == null) {
                throw new ConfigException("Cannot find transformer '" + transformerTO.getType() + "'");
            }
            if (transformer instanceof CipherTransformer) {
                // Dirty workaround
                transformerTO.getSettings().put(CipherTransformer.PROPERTY_MASTER_KEY, StringUtil.toHex(getMasterKey().getEncoded()));
                transformerTO.getSettings().put(CipherTransformer.PROPERTY_MASTER_KEY_SALT, StringUtil.toHex(getMasterKey().getSalt()));
            }
            transformer.init(transformerTO.getSettings());
            if (lastTransformer != null) {
                transformer.setNextTransformer(lastTransformer);
            }
            lastTransformer = transformer;
        }
        transformer = lastTransformer;
    }
}
Also used : NoTransformer(org.syncany.chunk.NoTransformer) CipherTransformer(org.syncany.chunk.CipherTransformer) Transformer(org.syncany.chunk.Transformer) NoTransformer(org.syncany.chunk.NoTransformer) TransformerTO(org.syncany.config.to.RepoTO.TransformerTO) CipherTransformer(org.syncany.chunk.CipherTransformer) ArrayList(java.util.ArrayList)

Aggregations

CipherTransformer (org.syncany.chunk.CipherTransformer)4 Transformer (org.syncany.chunk.Transformer)4 ArrayList (java.util.ArrayList)2 NoTransformer (org.syncany.chunk.NoTransformer)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 LinkedList (java.util.LinkedList)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 Chunker (org.syncany.chunk.Chunker)1 GzipTransformer (org.syncany.chunk.GzipTransformer)1 MultiChunker (org.syncany.chunk.MultiChunker)1 TttdChunker (org.syncany.chunk.TttdChunker)1 ZipMultiChunker (org.syncany.chunk.ZipMultiChunker)1 TransformerTO (org.syncany.config.to.RepoTO.TransformerTO)1 CipherSpec (org.syncany.crypto.CipherSpec)1 DefaultHandler (org.xml.sax.helpers.DefaultHandler)1