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