Coverage Report - org.mule.module.pgp.EncryptStreamTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
EncryptStreamTransformer
0%
0/31
0%
0/8
2
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.pgp;
 8  
 
 9  
 import java.io.IOException;
 10  
 import java.io.InputStream;
 11  
 import java.io.OutputStream;
 12  
 import java.security.SecureRandom;
 13  
 import java.util.Date;
 14  
 
 15  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
 16  
 
 17  
 import org.apache.commons.lang.Validate;
 18  
 import org.bouncycastle.bcpg.ArmoredOutputStream;
 19  
 import org.bouncycastle.openpgp.PGPCompressedData;
 20  
 import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
 21  
 import org.bouncycastle.openpgp.PGPEncryptedData;
 22  
 import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
 23  
 import org.bouncycastle.openpgp.PGPLiteralData;
 24  
 import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
 25  
 import org.bouncycastle.openpgp.PGPPublicKey;
 26  
 
 27  
 public class EncryptStreamTransformer implements StreamTransformer
 28  
 {
 29  
     private static final long offset = 1 << 24;
 30  
     
 31  
     private InputStream toBeEncrypted;
 32  
     private PGPPublicKey publicKey;
 33  
 
 34  
     private OutputStream pgpOutputStream;
 35  
     private OutputStream compressedEncryptedOutputStream;
 36  
     private OutputStream encryptedOutputStream;
 37  
     private OutputStream armoredOut;
 38  
     private long bytesWrote;
 39  
 
 40  
     public EncryptStreamTransformer(InputStream toBeEncrypted, PGPPublicKey publicKey) throws IOException
 41  0
     {
 42  0
         Validate.notNull(toBeEncrypted, "The toBeEncrypted should not be null");
 43  0
         Validate.notNull(publicKey, "The publicKey should not be null");
 44  
 
 45  0
         this.toBeEncrypted = toBeEncrypted;
 46  0
         this.publicKey = publicKey;
 47  0
         this.bytesWrote = 0;
 48  0
     }
 49  
 
 50  
     /**
 51  
      * {@inheritDoc}
 52  
      */
 53  
     public void initialize(OutputStream out) throws Exception
 54  
     {
 55  0
         armoredOut = new ArmoredOutputStream(out);
 56  0
         PGPEncryptedDataGenerator encrDataGen = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, false,
 57  
             new SecureRandom(), "BC");
 58  0
         encrDataGen.addMethod(this.publicKey);
 59  0
         encryptedOutputStream = encrDataGen.open(armoredOut, new byte[1 << 16]);
 60  
 
 61  0
         PGPCompressedDataGenerator comprDataGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
 62  0
         compressedEncryptedOutputStream = comprDataGen.open(encryptedOutputStream);
 63  
 
 64  0
         PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
 65  0
         pgpOutputStream = lData.open(compressedEncryptedOutputStream, PGPLiteralData.BINARY, "stream",
 66  
             new Date(), new byte[1 << 16]);
 67  0
     }
 68  
 
 69  
     /**
 70  
      * {@inheritDoc}
 71  
      */
 72  
     public boolean write(OutputStream out, AtomicLong bytesRequested) throws Exception
 73  
     {
 74  0
         int len = 0;
 75  0
         byte[] buf = new byte[1 << 16];
 76  0
         boolean wroteSomething = false;
 77  
         
 78  0
         while (bytesRequested.get() + offset > bytesWrote && (len = this.toBeEncrypted.read(buf)) > 0)
 79  
         {
 80  0
             pgpOutputStream.write(buf, 0, len);
 81  0
             bytesWrote = bytesWrote + len;
 82  0
             wroteSomething = true;
 83  
         }
 84  
 
 85  0
         if (wroteSomething && len <= 0)
 86  
         {
 87  0
             pgpOutputStream.close();
 88  0
             compressedEncryptedOutputStream.close();
 89  0
             encryptedOutputStream.close();
 90  0
             armoredOut.close();
 91  0
             toBeEncrypted.close();
 92  0
             return true;
 93  
         }
 94  
 
 95  0
         return false;
 96  
     }
 97  
 }