Coverage Report - org.mule.impl.security.SecretKeyEncryptionStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
SecretKeyEncryptionStrategy
0%
0/22
0%
0/2
1.556
 
 1  
 /*
 2  
  * $Id: SecretKeyEncryptionStrategy.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.impl.security;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.umo.lifecycle.InitialisationException;
 15  
 import org.mule.util.ObjectFactory;
 16  
 import org.mule.util.StringMessageUtils;
 17  
 
 18  
 import java.security.GeneralSecurityException;
 19  
 import java.security.spec.AlgorithmParameterSpec;
 20  
 import java.security.spec.KeySpec;
 21  
 
 22  
 import javax.crypto.KeyGenerator;
 23  
 import javax.crypto.SecretKey;
 24  
 import javax.crypto.spec.SecretKeySpec;
 25  
 
 26  
 /**
 27  
  * SecretKey based encryption using JCE. Users must specify a key as an array of
 28  
  * bytes. This can be set directly on the strategy or a keyFactory can be specified.
 29  
  * A keyFactory is an implementation of org.mule.util.ObjectFactory and must return a
 30  
  * byte array. The default algorthm used by this strategy is Blowfish, but users can
 31  
  * specify any valid algorithm supported by JCE.
 32  
  * 
 33  
  * @see ObjectFactory
 34  
  */
 35  
 public class SecretKeyEncryptionStrategy extends AbstractJCEEncryptionStrategy
 36  
 {
 37  
 
 38  
     public static final String DEFAULT_ALGORITHM = "Blowfish";
 39  
 
 40  
     private byte[] key;
 41  
     private ObjectFactory keyFactory;
 42  
 
 43  
     public SecretKeyEncryptionStrategy()
 44  0
     {
 45  0
         algorithm = DEFAULT_ALGORITHM;
 46  0
     }
 47  
 
 48  
     public void initialise() throws InitialisationException
 49  
     {
 50  0
         if (key == null)
 51  
         {
 52  0
             if (keyFactory == null)
 53  
             {
 54  0
                 throw new InitialisationException(CoreMessages.objectIsNull("Key / KeyFactory"), this);
 55  
             }
 56  
             else
 57  
             {
 58  
                 try
 59  
                 {
 60  0
                     key = (byte[]) keyFactory.create();
 61  
                 }
 62  0
                 catch (Exception e)
 63  
                 {
 64  0
                     throw new InitialisationException(e, this);
 65  0
                 }
 66  
             }
 67  
         }
 68  0
         super.initialise();
 69  0
     }
 70  
 
 71  
     protected KeySpec createKeySpec()
 72  
     {
 73  0
         return new SecretKeySpec(key, algorithm);
 74  
     }
 75  
 
 76  
     protected AlgorithmParameterSpec createAlgorithmParameterSpec()
 77  
     {
 78  0
         return null;
 79  
     }
 80  
 
 81  
     public void setKey(byte[] rawKey)
 82  
     {
 83  0
         this.key = rawKey;
 84  0
     }
 85  
 
 86  
     public void setKey(String rawKey)
 87  
     {
 88  0
         this.key = StringMessageUtils.getBytes(rawKey);
 89  0
     }
 90  
 
 91  
     public ObjectFactory getKeyFactory()
 92  
     {
 93  0
         return keyFactory;
 94  
     }
 95  
 
 96  
     public void setKeyFactory(ObjectFactory keyFactory)
 97  
     {
 98  0
         this.keyFactory = keyFactory;
 99  0
     }
 100  
 
 101  
     protected SecretKey getSecretKey() throws GeneralSecurityException
 102  
     {
 103  0
         return KeyGenerator.getInstance(algorithm).generateKey();
 104  
     }
 105  
 
 106  
 }