Coverage Report - org.apache.fulcrum.pbe.PBEServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PBEServiceImpl
0%
0/24
0%
0/2
1.083
 
 1  
 package org.apache.fulcrum.pbe;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.io.InputStream;
 24  
 import java.io.OutputStream;
 25  
 import java.security.GeneralSecurityException;
 26  
 
 27  
 import org.apache.avalon.framework.configuration.Configurable;
 28  
 import org.apache.avalon.framework.configuration.Configuration;
 29  
 import org.apache.avalon.framework.configuration.ConfigurationException;
 30  
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 31  
 import org.apache.fulcrum.jce.crypto.CryptoParameters;
 32  
 import org.apache.fulcrum.jce.crypto.CryptoStreamFactory;
 33  
 import org.apache.fulcrum.jce.crypto.CryptoStreamFactoryImpl;
 34  
 import org.apache.fulcrum.jce.crypto.CryptoUtil;
 35  
 import org.apache.fulcrum.jce.crypto.HexConverter;
 36  
 import org.apache.fulcrum.jce.crypto.PasswordFactory;
 37  
 import org.apache.fulcrum.jce.crypto.PasswordParameters;
 38  
 
 39  
 /**
 40  
  * Encapsulates an PBE (Password Based Encryption) functionality
 41  
  * from the JCE (Java Crypto Extension).
 42  
  *
 43  
  * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
 44  
  */
 45  
 
 46  
 public class PBEServiceImpl
 47  
     extends AbstractLogEnabled
 48  
     implements PBEService, Configurable
 49  
 {
 50  
     /** the internally used factory to create cipher streams */
 51  
     private CryptoStreamFactory cryptoStreamFactory;
 52  
 
 53  
     /** the salt for generating the password */
 54  
     private byte[] passwordSalt;
 55  
 
 56  
     /** the invocations of MessageDigest */
 57  
     private int passwordCount;
 58  
 
 59  
     /** the default password */
 60  
     private char[] defaultPassword;
 61  
 
 62  
     /**
 63  
      * Constructor
 64  
      */
 65  
     public PBEServiceImpl()
 66  0
     {
 67  
         // nothing to do
 68  0
     }
 69  
 
 70  
     /////////////////////////////////////////////////////////////////////////
 71  
     // Avalon Service Lifecycle Implementation
 72  
     /////////////////////////////////////////////////////////////////////////
 73  
 
 74  
     /**
 75  
      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
 76  
      */
 77  
     public void configure(Configuration configuration)
 78  
         throws ConfigurationException
 79  
     {
 80  
         // read the parameters for CryptoStreamFactory
 81  
 
 82  0
         byte[] cryptoSalt = CryptoParameters.SALT;
 83  0
         int cryptoCount = configuration.getChild("cyrptoCount").getValueAsInteger(CryptoParameters.COUNT);
 84  0
         String tempCryptoSalt = configuration.getChild("cryptoSalt").getValue("");
 85  
 
 86  0
         if( tempCryptoSalt.length() > 0 )
 87  
         {
 88  0
             cryptoSalt = HexConverter.toBytes( tempCryptoSalt );
 89  
         }
 90  
 
 91  
         // create the CryptoStreamFactory to be used
 92  
 
 93  0
         this.cryptoStreamFactory = new CryptoStreamFactoryImpl(
 94  
             cryptoSalt,
 95  
             cryptoCount
 96  
             );
 97  
 
 98  
         // read the parameters for PasswordFactory
 99  
 
 100  0
         this.passwordSalt = PasswordParameters.SALT;
 101  0
         this.passwordCount = configuration.getChild("passwordCount").getValueAsInteger(PasswordParameters.COUNT);
 102  0
         this.defaultPassword = PasswordParameters.DEFAULTPASSWORD;
 103  0
     }
 104  
 
 105  
 
 106  
     /////////////////////////////////////////////////////////////////////////
 107  
     // PBE Service Implementation
 108  
     /////////////////////////////////////////////////////////////////////////
 109  
 
 110  
     /**
 111  
      * @see org.apache.fulcrum.pbe.PBEService#createPassword()
 112  
      */
 113  
     public char[] createPassword() throws Exception
 114  
     {
 115  0
         return PasswordFactory.create(
 116  
             this.defaultPassword,
 117  
             this.passwordSalt,
 118  
             this.passwordCount
 119  
             );
 120  
     }
 121  
 
 122  
     /**
 123  
      * @see org.apache.fulcrum.pbe.PBEService#createPassword(char[])
 124  
      */
 125  
     public char [] createPassword(char [] seed) throws Exception
 126  
     {
 127  0
         return PasswordFactory.create(
 128  
             seed,
 129  
             this.passwordSalt,
 130  
             this.passwordCount
 131  
             );
 132  
     }
 133  
 
 134  
     /**
 135  
      * @see org.apache.fulcrum.pbe.PBEService#decryptString(java.lang.String, char[])
 136  
      */
 137  
     public String decryptString(String cipherText, char [] password)
 138  
         throws GeneralSecurityException, IOException
 139  
     {
 140  0
         return CryptoUtil.decryptString(
 141  
             this.getCryptoStreamFactory(),
 142  
             cipherText,
 143  
             password
 144  
             );
 145  
     }
 146  
 
 147  
     /**
 148  
      * @see org.apache.fulcrum.pbe.PBEService#encryptString(java.lang.String, char[])
 149  
      */
 150  
     public String encryptString(String plainText, char [] password)
 151  
         throws GeneralSecurityException, IOException
 152  
     {
 153  0
         return CryptoUtil.encryptString(
 154  
             this.getCryptoStreamFactory(),
 155  
             plainText,
 156  
             password
 157  
             );
 158  
     }
 159  
 
 160  
     /**
 161  
      * @see org.apache.fulcrum.pbe.PBEService#getInputStream(java.io.InputStream, char[])
 162  
      */
 163  
     public InputStream getInputStream(InputStream is, char [] password)
 164  
         throws GeneralSecurityException, IOException
 165  
     {
 166  0
         return this.getCryptoStreamFactory().getInputStream(
 167  
             is,
 168  
             password
 169  
             );
 170  
     }
 171  
 
 172  
     /**
 173  
      * @see org.apache.fulcrum.pbe.PBEService#getSmartInputStream(java.io.InputStream, char[])
 174  
      */
 175  
     public InputStream getSmartInputStream(InputStream is, char [] password)
 176  
         throws GeneralSecurityException, IOException
 177  
     {
 178  0
         return this.getCryptoStreamFactory().getSmartInputStream(
 179  
             is,
 180  
             password
 181  
             );
 182  
     }
 183  
 
 184  
     /**
 185  
      * @see org.apache.fulcrum.pbe.PBEService#getOutputStream(java.io.OutputStream, char[])
 186  
      */
 187  
     public OutputStream getOutputStream(OutputStream os, char [] password)
 188  
         throws GeneralSecurityException, IOException
 189  
     {
 190  0
         return this.getCryptoStreamFactory().getOutputStream(
 191  
             os,
 192  
             password
 193  
             );
 194  
     }
 195  
 
 196  
     /**
 197  
      * @see org.apache.fulcrum.pbe.PBEService#decrypt(java.lang.Object, java.lang.Object, char[])
 198  
      */
 199  
     public void decrypt(Object source, Object target, char [] password)
 200  
         throws GeneralSecurityException, IOException
 201  
     {
 202  0
         CryptoUtil.decrypt(
 203  
             this.getCryptoStreamFactory(),
 204  
             source,
 205  
             target,
 206  
             password
 207  
             );
 208  0
     }
 209  
 
 210  
     /**
 211  
      * @see org.apache.fulcrum.pbe.PBEService#encrypt(java.lang.Object, java.lang.Object, char[])
 212  
      */
 213  
     public void encrypt(Object source, Object target, char [] password)
 214  
         throws GeneralSecurityException, IOException
 215  
     {
 216  0
         CryptoUtil.encrypt(
 217  
             this.getCryptoStreamFactory(),
 218  
             source,
 219  
             target,
 220  
             password
 221  
             );
 222  0
     }
 223  
 
 224  
     /////////////////////////////////////////////////////////////////////////
 225  
     // Service Implementation
 226  
     /////////////////////////////////////////////////////////////////////////
 227  
 
 228  
     /**
 229  
      * @return Returns the cryptoStreamFactory.
 230  
      */
 231  
     private CryptoStreamFactory getCryptoStreamFactory()
 232  
     {
 233  0
         return cryptoStreamFactory;
 234  
     }
 235  
 }