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