Coverage Report - org.mule.impl.security.PasswordBasedEncryptionStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
PasswordBasedEncryptionStrategy
71%
17/24
50%
2/4
1.3
 
 1  
 /*
 2  
  * $Id: PasswordBasedEncryptionStrategy.java 7963 2007-08-21 08:53:15Z 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  
 
 16  
 import java.security.GeneralSecurityException;
 17  
 import java.security.spec.AlgorithmParameterSpec;
 18  
 import java.security.spec.KeySpec;
 19  
 
 20  
 import javax.crypto.SecretKey;
 21  
 import javax.crypto.SecretKeyFactory;
 22  
 import javax.crypto.spec.PBEKeySpec;
 23  
 import javax.crypto.spec.PBEParameterSpec;
 24  
 
 25  
 /**
 26  
  * Provides password-based encryption using JCE. Users must specify a password and
 27  
  * optionally a salt and iteration count as well. The default algorithm is
 28  
  * PBEWithMD5AndDES, but users can specify any valid algorithm supported by JCE.
 29  
  */
 30  
 public class PasswordBasedEncryptionStrategy extends AbstractJCEEncryptionStrategy
 31  
 {
 32  
 
 33  
     public static final String DEFAULT_ALGORITHM = "PBEWithMD5AndDES";
 34  
     public static final int DEFAULT_ITERATION_COUNT = 20;
 35  
 
 36  14
     private byte[] salt = null;
 37  
 
 38  14
     private int iterationCount = DEFAULT_ITERATION_COUNT;
 39  
 
 40  
     private char[] password;
 41  
 
 42  
     public PasswordBasedEncryptionStrategy()
 43  14
     {
 44  14
         algorithm = DEFAULT_ALGORITHM;
 45  14
     }
 46  
 
 47  
     public void initialise() throws InitialisationException
 48  
     {
 49  14
         if (salt == null)
 50  
         {
 51  14
             salt = new byte[]{(byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8,
 52  
                 (byte) 0xee, (byte) 0x99};
 53  14
             logger.debug("Salt is not set. Using default salt");
 54  
         }
 55  
 
 56  14
         if (password == null)
 57  
         {
 58  0
             throw new InitialisationException(CoreMessages.objectIsNull("Password"), this);
 59  
         }
 60  14
         super.initialise();
 61  14
     }
 62  
 
 63  
     protected KeySpec createKeySpec()
 64  
     {
 65  14
         return new PBEKeySpec(password);
 66  
     }
 67  
 
 68  
     protected AlgorithmParameterSpec createAlgorithmParameterSpec()
 69  
     {
 70  14
         return new PBEParameterSpec(salt, iterationCount);
 71  
     }
 72  
 
 73  
     public byte[] getSalt()
 74  
     {
 75  0
         return salt;
 76  
     }
 77  
 
 78  
     public void setSalt(byte[] salt)
 79  
     {
 80  0
         this.salt = salt;
 81  0
     }
 82  
 
 83  
     public int getIterationCount()
 84  
     {
 85  0
         return iterationCount;
 86  
     }
 87  
 
 88  
     public void setIterationCount(int iterationCount)
 89  
     {
 90  0
         this.iterationCount = iterationCount;
 91  0
     }
 92  
 
 93  
     public void setPassword(String password)
 94  
     {
 95  14
         this.password = password.toCharArray();
 96  14
     }
 97  
 
 98  
     protected SecretKey getSecretKey() throws GeneralSecurityException
 99  
     {
 100  14
         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(getAlgorithm());
 101  14
         return keyFactory.generateSecret(keySpec);
 102  
     }
 103  
 }